How can I debug PHP cURL sessions?

Setting CURLOPT_HEADER and CURLOPT_VERBOSE to 'true' are two of the most important ways to debug PHP cURL sessions. 

Detailed Description

PHP’s cURL library is widely used by PHP applications to communicate with the eBay API. However, debugging cURL sessions can be problematic. 

Since many of eBay’s services require proper information in the HTTP headers, verifying correct header transfer is important. This can be done by setting the CURLOPT_HEADER to 'true'. 

Setting CURLOPT_VERBOSE to 'true' will display two-way communication between the PHP application and the server. 
You can also use curl_getinfo to get error messages. 

Sample Code and Session for CURLOPT_VERBOSE

 <?php
 $headers = array (
 'X-EBAY-API-COMPATIBILITY-LEVEL: 507',
 'X-EBAY-API-DEV-NAME: ABCD',
 'X-EBAY-API-APP-NAME: EFGH',
 'X-EBAY-API-CERT-NAME: IJKL',
 'X-EBAY-API-SITEID: 0',
 );
 $session = curl_init();
 curl_setopt($session, CURLOPT_URL, "http://ebay.com/ws/api.dll"); // Oops - wrong URL for API
 curl_setopt($session, CURLOPT_HTTPHEADER, $headers); // Set headers to above array
 curl_setopt($session, CURLOPT_HEADER, true); // Display headers
 curl_setopt($session, CURLOPT_VERBOSE, true); // Display communication with server
 curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Return data instead of display to std out
 curl_exec($session);
 curl_close($session);
?>


C:\>php curlDebug.php * About to connect() to ebay.com port 80
* Trying 66.135.192.87... * connected
* Connected to ebay.com (66.135.192.87) port 80
> GET /ws/api.dll HTTP/1.1
Host: ebay.com
Accept: */*
X-EBAY-API-COMPATIBILITY-LEVEL: 507
X-EBAY-API-DEV-NAME: ABCD
X-EBAY-API-APP-NAME: EFGH
X-EBAY-API-CERT-NAME: IJKL
X-EBAY-API-SITEID: 0
< HTTP/1.1 301 Moved Permanently
< Date: Fri, 01 Jun 2007 06:16:15 GMT
< Server: Microsoft-IIS/6.0
< Location: http://pages.ebay.com/ws/api.dll
* Connection #0 to host ebay.com left intact
* Closing connection #0
C:>


In the above, lines that start with * are cURL informational messages. Lines that start with > are headers sent by cURL. Lines that start with < are headers sent by the server, 

Another way to debug : curl_getinfo


Assume you have the following php file to be executed on a web server. Note the debug statements :

<?php
$ch = curl_init("http://www.ebayXYZ.com/"); // simple get on ebay.com home page - oops wrong host
$fp = fopen("ebay_homepage.html", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);

print "<pre>\n";
print_r(curl_getinfo($ch));  // get error info
echo "\n\ncURL error number:" .curl_errno($ch); // print error info
echo "\n\ncURL error:" . curl_error($ch); 
print "</pre>\n";
curl_close($ch); // close curl session
fclose($fp);
?>


OUTPUT :
Array
(
 [url] => http://www.ebayXYZ.com/
 [http_code] => 0
 [header_size] => 0
 [request_size] => 0
 [filetime] => -1
 [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] => 0
 [upload_content_length] => 0
 [starttransfer_time] => 0
 [redirect_time] => 0
)

cURL error number:6
cURL error:Could not resolve host: www.ebayXYZ.com; Host not found

Comments

Popular posts from this blog

Free icons

how to create template file in php. why it is usefull