15.10.4 Debugging CurlThis is NOT the latest copy of this book; click here for the latest version.
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 (64.246.30.37) 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/1.3.26.1a 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%2C213.152.58.41; 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 see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|