Skip to main content

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

Popular posts from this blog

Draw pyramid shape of stars in PHP using loop structure Write down the code in PHP using loop structure to draw the 3 pyramid shapes of stars as given bellow:

ravimanephpmysql

// problem 01 solution
for($i=5;$i>=1;$i--)
{
echo  str_repeat('*',$i);
echo "<br />";
}

echo "<hr />";

// problem 02 solution

for($i=1;$i<=5;$i++)
{
echo  str_repeat('*',$i);
echo "<br />";
}


echo "<hr />";
// problem 03 solution

$j=1;

for($i=5;$i>=1;$i--)
{
//echo  str_repeat("&nbsp;",$i-1);
echo  str_repeat('*',$j++);
echo "<br />";
}

Google reCAPTCHA with PHP tutorial

Login to your Google account and submit the form. Once submit, Google will provide you following two information. Site keySecret key

Integrate it into your website. To integrate it into your website you need to put it in client side as well as in Server side. In client HTML page you need to integrate this line before <HEAD> tag.
<script src='https://www.google.com/recaptcha/api.js'></script> And to show the widget into your form you need to put this below contact form, comment form etc.
<divclass="g-recaptcha" data-sitekey="== Your site Key =="></div> When the form get submit to Server, this script will send ‘g-recaptcha-response’ as a POST data. You need to verify it in order to see whether user has checked the Captcha or not. Simple comment form with Google reCAPTCH:Index.html <html>
<head>
<title>Google recapcha demo - Codeforgeek</title>
<scriptsrc='https://www.google.com/recaptcha/api.js'&g…

Simple Laravel CRUD with Resource Controllers

Creating, reading, updating, and deleting resources is used in pretty much every application. Laravel helps make the process easy using resource controllers. Resource Controllers can make life much easier and takes advantage of some cool Laravel routing techniques. Today, we'll go through the steps necessary to get a fully functioning CRUD application using resource controllers. For this tutorial, we will go through the process of having an admin panel to create, read, update, and delete (CRUD) a resource. Let's use nerds as our example. We will also make use of Eloquent ORM. This tutorial will walk us through: * Setting up the database and models * Creating the resource controller and its routes * Creating the necessary views * Explaining each method in a resource controller Table of ContentsGetting our Database ReadyEloquent Model for the NerdsCreating the ControllerSetting Up the RoutesThe ViewsMaking It All Work TogetherConclusion To get started, we will need the controll…