As it works with so many different network protocols, it is very easy to make mistakes when using Curl. You can speed up your debugging efforts by using CURLOPT_VERBOSE to have Curl output detailed information about its actions.
To give you an idea of how CURLOPT_VERBOSE affects the output of your script, here is a script we used earlier rewritten to add CURLOPT_VERBOSE:
<?php $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, "http://www.php.net"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_VERBOSE, 1); curl_exec ($curl); curl_close ($curl); ?>
Note that CURLOPT_RETURNTRANSFER was used but the output from curl_exec() was ignored - this is because the extra data provided by CURLOPT_VERBOSE is actually sent straight to the browser irrespective of CURLOPT_RETURNTRANSFER, so by ignoring the output of curl_exec() the script will only print out the debugging information. Here is what you should get:
* About to connect() to www.php.net:80
* Connected to php.net (22.214.171.124) port 80
> GET / HTTP/1.1 Host: www.php.net Pragma: no-cache Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
< HTTP/1.1 200 OK < Date: Fri, 06 Feb 2004 22:13:29 GMT
< Server: Apache/1.3.26 (Unix) mod_gzip/126.96.36.199a PHP/4.3.3-dev
< X-Powered-By: PHP/4.3.3-dev
< Last-Modified: Fri, 06 Feb 2004 22:14:38 GMT
< Content-language: en
< Set-Cookie: COUNTRY=GBR%2C188.8.131.52; expires=Fri, 13-Feb-04 22:13:29 GMT; path=/; domain=.php.net
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html;charset=ISO-8859-1
* Closing connection #0
Note that lines that start with > are headers sent by Curl, lines that start with < are headers sent by the responding server, and lines that start with * are Curl informational messages.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Custom data stream handling >>
Previous chapter: The abridged list of Curl options
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.