Strange behavior of cURL request in PHP script
I am experiencing strange behavior of a cURL request that I am making in my PHP code. I am 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 execute this little PHP code snippet through my CLI (Powershell since I am on Windows) everything is fine , all var_dumps are working and I can see the $ header and $ body and everything and the data I expect is really are present.
Now for the weird behavior. If I open 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 am completely puzzled as I cannot see the difference between script beeing running CLI and running browser. Anyone got an idea about this?
EDIT Note. If I use Guzzle for the request, everything works fine, both in the CLI and in the browser. I'm still interested in the answer why cURL is causing problems here.
source to share
Have you tried verbose output for Curl's query?
I usually think this is the best way to figure out what's going on under the hood ... fooobar.com/questions/42395 / ...
Also not that popular, but this approach looks simple to implement and is much cleaner ... fooobar.com/questions/42395 / ...
source to share