-1

I have this test code:

<?php

$html_brand = "https://google.com";
$ch = curl_init();

$options = array(
    CURLOPT_URL            => $html_brand,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING       => "",
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 120,
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_MAXREDIRS      => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ( $httpCode != 200 ){
    echo "Return code is {$httpCode} \n"
        .curl_error($ch);
    print_r(curl_getinfo($ch));
} else {
    echo "<pre>".htmlspecialchars($response)."</pre>";
}

curl_close($ch);

And page result (I have put \n before variable name for better readability) is:

Return code is 0 Array ( 
[url] => https://google.com 
[content_type] => 
[http_code] => 0 
[header_size] => 0 
[request_size] => 0 
[filetime] => 0 
[ssl_verify_result] => 0 
[redirect_count] => 0 
[total_time] => 0 
[namelookup_time] => 0 
[connect_time] => 0 
[pretransfer_time] => 0 
[size_upload] => 0 
[size_download] => 0 
[speed_download] => 0 
[speed_upload] => 0 
[download_content_length] => -1 
[upload_content_length] => -1 
[starttransfer_time] => 0 
[redirect_time] => 0 
[redirect_url] => 
[primary_ip] => 
[certinfo] => Array ( ) 
[primary_port] => 0 
[local_ip] => 
[local_port] => 0 ) 

What is wrong here? I could not figure out why curl is not working on my machine. Internet is working, command line curl is working very fine. I'm on PHP Version 5.5.9-1ubuntu4.14, and this is curl section from phpinfo()phpinfo screenshot

6
  • Have you tried dumping curl error var_dump(curl_error($ch));? Commented Mar 25, 2016 at 17:06
  • var dump is exactly string(0) "" Commented Mar 25, 2016 at 17:08
  • stackoverflow.com/questions/10227879/… Commented Mar 25, 2016 at 17:10
  • but that answer doesn't help me, because I don't know what is wrong. curl doesn't return anything useful to determine what's wrong Commented Mar 25, 2016 at 17:14
  • There's likely nothing wrong with your code (I tried it, straight copy paste worked) you're likely getting blocked by a firewall or something. A status code of 0 usually means the server was never reached. You could try ping google.com from your server's console. Check out this question stackoverflow.com/questions/10227879/… Commented Mar 25, 2016 at 17:16

1 Answer 1

1

I have found out that I have disabled curl_exec function in php.ini. In apache2 log:

PHP Warning:  curl_exec() has been disabled for security reasons in /var/www/***/html/test.php on line 18

Enabling it in php.ini did the trick!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.