Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Steps do you Take to Troubleshoot Problems with PHP cURL?

Almost any working PHP programmer has faced having to use CURL to send raw HTTP requests, whether it's for credit card payment processing, nefarious screen scraping, or something in-between.

Almost any forum where PHP programmers congregate has a large number of people who can't get the cURL functions to do what they want.

When cURL isn't working for you, what troubleshooting techniques do you use to figure out why it isn't working? What weird gotchas with PHP's curl implementation have you run into? If someone asks a "HALP MY CURL IZ BROKEN" question on a forum, what are the steps you take to figure out why their request isn't working?

like image 919
Alan Storm Avatar asked May 02 '09 23:05

Alan Storm


People also ask

What is the use of curl function in PHP?

PHP’s cURL functions are extremely useful for sending HTTP requests. Some examples of its usefulness. Retrieving data from an external API. Sending data to an external web service. Checking to see if a HTTP resource exists. Crawling / scraping web pages ( logging into other websites with PHP ).

How do I fix curl failed with PHP5?

CURL failed with PHP5.3 and Apache2.2.X on my Windows 7 machine. It turns out that it's not enough to copy the two dll's mentioned (libeay32 and sslea32) from the php folder into your system32 folder. You HAVE TO UNBLOCK THESE TWO FILES. Right click the file, select unblock, for each one. Then restart Apache.

How do I use curl to troubleshoot network connectivity?

Using curl to troubleshoot To use curl to test basic network connectivity, you need to know several things: The remote server name or IP address. The protocol for the service to be tested (HTTP, FTP, SMTP, etc.)

How do I know if a curl request has failed?

The curl_errno function will return the number 0 (zero) if the request was successful. In other words, it will return a “falsey” value if no error occurs. This is because PHP sees 0 as a false value in a boolean context. This allows us to figure out whether or not our cURL request resulted in an error.


1 Answers

I find the CURLINFO_HEADER_OUT option to be very useful.

<?php
$curl = curl_init('http://www.php.net');

curl_setopt($curl, CURLOPT_HEADERFUNCTION, 'dbg_curl_data');
curl_setopt($curl, CURLOPT_WRITEFUNCTION, 'dbg_curl_data');
curl_setopt($curl, CURLINFO_HEADER_OUT, true);

curl_exec($curl);

echo '<fieldset><legend>request headers</legend>
  <pre>', htmlspecialchars(curl_getinfo($curl, CURLINFO_HEADER_OUT)), '</pre>
</fieldset>';

echo '<fieldset><legend>response</legend>
  <pre>', htmlspecialchars(dbg_curl_data(null)), '</pre>
</fieldset>';

function dbg_curl_data($curl, $data=null) {
  static $buffer = '';

  if ( is_null($curl) ) {
    $r = $buffer;
    $buffer = '';
    return $r;
  }
  else {
    $buffer .= $data;
    return strlen($data);
  }
}
like image 81
VolkerK Avatar answered Oct 01 '22 21:10

VolkerK