3

I'm experiencing weird behaviour of a cURL request that I do in my PHP code. I'm running the code locally on a standard WAMP Apache server. Here's the code:

$auth_info = "...";
$some_url = "...";
$channel = curl_init();
curl_setopt($channel, CURLOPT_URL, $some_url);
curl_setopt($channel, CURLOPT_HTTPHEADER, 
    array("Authorization: BASIC " . base64_encode($auth_info))
);
curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
curl_setopt($channel, CURLOPT_HEADER, true);
$response = curl_exec($channel);
var_dump(curl_getinfo($channel));
$header_size = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
var_dump($header);
var_dump($body);
curl_close($channel);

If I'm executing this little PHP code snippet via my CLI (Powershell, since I'm running on Windows), everything is fine, all var_dumps work and I can see the $header and $body and everything and the data I expect is actually present.

Now for the weird behaviour. If I'm opening the script file with the above snippet in any browser, it just gives me:

array (size=26)
'url' => string 'the_url_here' (length=39)
'content_type' => null
'http_code' => int 0
'header_size' => int 0
'request_size' => int 0
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 1.234
'namelookup_time' => float 0
'connect_time' => float 0.109
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float -1
'starttransfer_time' => float 0
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string 'here_is_the_ip' (length=12)
'certinfo' => 
  array (size=0)
    empty
'primary_port' => int 443
'local_ip' => string 'here_is_my_ip' (length=13)
'local_port' => int -> my_local_port_here
boolean false
boolean false

I'm completely puzzled since I cannot see a difference between the script beeing started by the CLI and beeing started by the browser. Has anyone got an idea on this?

EDIT Note: If I'm using Guzzle for the request, everything works fine, both in CLI and browser. I'm still interested in an answer why cURL is causing problems here.

7
  • Maybe you need a correct URL to run the test on? Commented Apr 28, 2015 at 9:13
  • I suspect your CLI and WAMP are pointing to different PHP installs. Write a quick page using php_info() and see where the php executable is for both. Commented Apr 28, 2015 at 9:14
  • I cannot find direct paths to the php.exe in any of the php_info() outputs, but I can confirm, that browser and CLI have different values for "Loaded Configuration File". That could already be it. Commented Apr 28, 2015 at 9:36
  • @DevanLoper what is the script/command that executes this script? Commented Apr 28, 2015 at 13:20
  • @sitilge the script is stored in an example.php file, in Powershell it starts with "php example.php" and in the browser I'm using "localhost/example.php". is that what you wanted to know? Commented Apr 28, 2015 at 13:35

1 Answer 1

1

Have you tried logging verbose output for the Curl request?

Normally I find this the best way to figure out what's going on under the hood...https://stackoverflow.com/a/14436877/682754

Also not as popular but this approach looks simple to implement and is a lot cleaner...https://stackoverflow.com/a/26611776/682754

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

2 Comments

You were right. Verbosely debugging the request lead me to this error: "SSL certificate problem: self signed certificate in certificate chain". Using either of the solutions proposed here: stackoverflow.com/questions/6400300/… can help to bypass this issue. I'll accept your answer, since it actually pointed me to the solution, even if it was indirectly ;-)
Good to hear you found the solution, certificates have been the cause of many a curl problem for me too

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.