分类:
2008-04-22 12:08:17
cURL stands for "Client URLs", and was developed by Daniel Stenberg in 1998 as a command line tool. libcurl is a portable library that provides an easy interface to the cURL functionality. It is thread safe, IPv6 compatible, and supports persistent connections. The libcurl PHP binding was added by Sterling Hughes.
Both cURL and libcurl can transfer files using a wide variety of protocols, including HTTP, HTTPS, FTP, FTPS, GOPHER, LDAP, DICT, TELNET and FILE. The libraries run on practically any *NIX operating system, as well as Windows, OS/2, BeOS, and many more.
The cURL libraries are truly open source, with an MIT/X derivative license. This license is very liberal, allowing the use of cURL for whatever you want, commercial or not. You can use libcurl for free, and even include and distribute it with your own application, whether commercial or closed-source.
cURL should not to be confused with the Curl Corporation, which is the commercial producer of the client side programming language, Curl.
From PHP version 4.2.3 on, you need a cURL version of at least 7.9.0. From PHP version 4.3.0 on, you need a cURL version of at least 7.9.8.
Windows:
As with any PHP extension in Windows, you will need the PHP distribution that includes external extensions. Once PHP is installed, you will need to copy the files php4ts.dll, ssleay32.dll, php_curl.dll, msvcrt.dll from the 'DLLs' folder to your Windows PATH, i.e.:
c:\windows\system for Windows 9x/Me
c:\winnt\system32 for Windows NT/2000
c:\windows\system32 for Windows XP
cURL can then be enabled by uncommenting the line 'extension=php_curl.dll' in the php.ini file. Alternatively you can load the module dynamically in your script using:
php
dl("php_curl.dll");
?>
Unix:
Your local mirror for downloading cURL can be found at http://curl.haxx.se. Precompiled binaries are also available for a wide range of operating systems.
Because cURL relies on the openssl
library for SSL connections, openssl
must be installed first. If openssl
is not installed, SSL support will be omitted from the cURL build. After installing cURL (./configure, make, make install), PHP must be recompiled to include cURL support (--with-curl
).
If cURL support is enabled, the phpinfo()
function will display it in its output.
Using cURL from the command line is extremely easy. The following example retrieves a web page and prints the page to stdout
:
$ curl -L zend.com
(The '-L' tells cURL to follow redirects.)
Of course, we can execute cURL on the command line using PHP. The following example does just that, and gets 3 pages at once:
php
$var = echo shell_exec("/usr/bin/curl -L http://zend.com/developers.php ");
?>
While using cURL from within PHP is an option, using the libcurl PHP binding is much easier, especially for things like an HTTP POST operation.
The process of using libcurl from within PHP is a matter of following these basic steps:
Following this process, here are a few examples demonstrating HTTP POST, HTTP Authentication, and FTP:
php
// FIND BOOKS ON PHP AND MYSQL ON AMAZON
$url = "";
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "url=index%3Dbooks&field-keywords=PHP+MYSQL"); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch);
echo $result;
?>
// HTTP authentication
$url = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
// FTP this script to a server
$fp = fopen(__FILE__, "r");
$url = "ftp://username:password@mydomain.com:21/path/to/newfile.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_FTPASCII, 1);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize(__FILE__));
$result = curl_exec($ch);
curl_close($ch);
?>
Tip: If you are having trouble getting libcurl to do what you want, add the following code before closing the cURL handle:
<pre>
php
print_r(curl_getinfo($ch));
echo "\n\ncURL error number:" .curl_errno($ch);
echo "\n\ncURL error:" . curl_error($ch);
// ...close cURL handle ($ch) below
?>
This will force libcurl to report back on what happened on the last transfer, making it easier to troubleshoot.
The decision as to whether to use cURL or libcurl depends on the situation. For instance, if I have a cron job running that e-mails me when a file changes on a remote server, or if my ISP doesn’t have libcurl support in their PHP install, using cURL makes more sense. However, if I have libcurl support in PHP and I am building a PHP application requiring cURL functionality, libcurl is the right choice.
The cURL libraries provide a nice interface for file transfers to and from a webserver. They have support for a wide variety of protocols (like HTTPS) giving them an edge over built-in PHP functions like fsockopen()
. The libraries are thread-safe, IPv6 compatible, and will work with any technology that is built on top of HTTP. Whether you are building simple script to fetch a web page, or a secure payment gateway, leveraging the functions built into cURL can save a lot of time.
cURL on Sourceforge http://curl.sourceforge.net/
cURL's man page - http://curl.sourceforge.net/docs/manpage.html
PHP/cURL Manual - /manual/ref.curl.php
Whatever spare time Jim has left after working full-time as a web developer, he spends developing websites like . Feel free to send any questions or comments to