Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Debugging Curl

People also ask

Can I use cURL in PHP?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.

What is Curlopt_verbose?

When CURLOPT_VERBOSE is set, output is written to STDERR or the file specified using CURLOPT_STDERR . The output is very informative. You can also use tcpdump or wireshark to watch the network traffic.

Is PHP cURL secure?

Curl is as secure as a normal HTTP request.


You can enable the CURLOPT_VERBOSE option and log that information to a (temporary) CURLOPT_STDERR:

// CURLOPT_VERBOSE: TRUE to output verbose information.
// Writes output to STDERR, 
// -or- the file specified using CURLOPT_STDERR.
curl_setopt($curlHandle, CURLOPT_VERBOSE, true);

$streamVerboseHandle = fopen('php://temp', 'w+');
curl_setopt($curlHandle, CURLOPT_STDERR, $streamVerboseHandle);

You can then read it after curl has done the request:

$result = curl_exec($curlHandle);
if ($result === FALSE) {
    printf("cUrl error (#%d): %s<br>\n",
           curl_errno($curlHandle),
           htmlspecialchars(curl_error($curlHandle)))
           ;
}

rewind($streamVerboseHandle);
$verboseLog = stream_get_contents($streamVerboseHandle);

echo "cUrl verbose information:\n", 
     "<pre>", htmlspecialchars($verboseLog), "</pre>\n";

(I originally answered similar but more extended in a related question.)

More information like metrics about the last request is available via curl_getinfo. This information can be useful for debugging curl requests, too. A usage example, I would normally wrap that into a function:

$version = curl_version();
extract(curl_getinfo($curlHandle));
$metrics = <<<EOD
URL....: $url
Code...: $http_code ($redirect_count redirect(s) in $redirect_time secs)
Content: $content_type Size: $download_content_length (Own: $size_download) Filetime: $filetime
Time...: $total_time Start @ $starttransfer_time (DNS: $namelookup_time Connect: $connect_time Request: $pretransfer_time)
Speed..: Down: $speed_download (avg.) Up: $speed_upload (avg.)
Curl...: v{$version['version']}
EOD;

You can enable the CURLOPT_VERBOSE option:

curl_setopt($curlhandle, CURLOPT_VERBOSE, true);

When CURLOPT_VERBOSE is set, output is written to STDERR or the file specified using CURLOPT_STDERR. The output is very informative.

You can also use tcpdump or wireshark to watch the network traffic.


Here is a simpler code for the same:

   curl_setopt($ch, CURLOPT_VERBOSE, 1);
   curl_setopt($ch, CURLOPT_STDERR, $fp);

where $fp is a file handle to output errors. For example:

   $fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');

( Read on http://curl.haxx.se/mail/curlphp-2008-03/0064.html )


Here is an even simplier way, by writing directly to php error output

curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, fopen('php://stderr', 'w'));