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?
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 ).
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.
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.)
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.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With