XII. CURL, Client URL Library Functions
Introduction
PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.
These functions have been added in PHP 4.0.2.
Requirements
In order to use the CURL functions you need to install the CURL package. PHP requires that you use CURL 7.0.2-beta or higher. PHP will not work with any version of CURL below version 7.0.2-beta. In PHP version 4.2.3, you will need CURL version 7.9.0 or higher. From PHP 4.3.0, you will need a CURL version that's 7.9.8 or higher. PHP 5.0.0 will most likely require a CURL version greater than 7.10.5
Installation
To use PHP's CURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories. In the "include" directory there should be a folder named "curl" which should contain the easy.h and curl.h files. There should be a file named libcurl.a located in the "lib" directory. Beginning with PHP 4.3.0 you can configure PHP to use CURL for url streams --with-curlwrappers.
Note to Win32 Users: In order to enable this module on a Windows environment, you must copy libeay32.dll and ssleay32.dll from the DLL folder of the PHP/Win32 binary package to the SYSTEM folder of your Windows machine. (Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM)
Predefined Constants
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
- CURLOPT_PORT (integer)
- CURLOPT_FILE (integer)
- CURLOPT_INFILE (integer)
- CURLOPT_INFILESIZE (integer)
- CURLOPT_URL (integer)
- CURLOPT_PROXY (integer)
- CURLOPT_VERBOSE (integer)
- CURLOPT_HEADER (integer)
- CURLOPT_HTTPHEADER (integer)
- CURLOPT_NOPROGRESS (integer)
- CURLOPT_NOBODY (integer)
- CURLOPT_FAILONERROR (integer)
- CURLOPT_UPLOAD (integer)
- CURLOPT_POST (integer)
- CURLOPT_FTPLISTONLY (integer)
- CURLOPT_FTPAPPEND (integer)
- CURLOPT_NETRC (integer)
- CURLOPT_FOLLOWLOCATION (integer)
- CURLOPT_FTPASCII (integer)
- CURLOPT_PUT (integer)
- CURLOPT_MUTE (integer)
- CURLOPT_USERPWD (integer)
- CURLOPT_PROXYUSERPWD (integer)
- CURLOPT_RANGE (integer)
- CURLOPT_TIMEOUT (integer)
- CURLOPT_POSTFIELDS (integer)
- CURLOPT_REFERER (integer)
- CURLOPT_USERAGENT (integer)
- CURLOPT_FTPPORT (integer)
- CURLOPT_LOW_SPEED_LIMIT (integer)
- CURLOPT_LOW_SPEED_TIME (integer)
- CURLOPT_RESUME_FROM (integer)
- CURLOPT_COOKIE (integer)
- CURLOPT_SSLCERT (integer)
- CURLOPT_SSLCERTPASSWD (integer)
- CURLOPT_WRITEHEADER (integer)
- CURLOPT_SSL_VERIFYHOST (integer)
- CURLOPT_COOKIEFILE (integer)
- CURLOPT_SSLVERSION (integer)
- CURLOPT_TIMECONDITION (integer)
- CURLOPT_TIMEVALUE (integer)
- CURLOPT_CUSTOMREQUEST (integer)
- CURLOPT_STDERR (integer)
- CURLOPT_TRANSFERTEXT (integer)
- CURLOPT_RETURNTRANSFER (integer)
- CURLOPT_QUOTE (integer)
- CURLOPT_POSTQUOTE (integer)
- CURLOPT_INTERFACE (integer)
- CURLOPT_KRB4LEVEL (integer)
- CURLOPT_HTTPPROXYTUNNEL (integer)
- CURLOPT_FILETIME (integer)
- CURLOPT_WRITEFUNCTION (integer)
- CURLOPT_READFUNCTION (integer)
- CURLOPT_PASSWDFUNCTION (integer)
- CURLOPT_HEADERFUNCTION (integer)
- CURLOPT_MAXREDIRS (integer)
- CURLOPT_MAXCONNECTS (integer)
- CURLOPT_CLOSEPOLICY (integer)
- CURLOPT_FRESH_CONNECT (integer)
- CURLOPT_FORBID_REUSE (integer)
- CURLOPT_RANDOM_FILE (integer)
- CURLOPT_EGDSOCKET (integer)
- CURLOPT_CONNECTTIMEOUT (integer)
- CURLOPT_SSL_VERIFYPEER (integer)
- CURLOPT_CAINFO (integer)
- CURLOPT_COOKIEJAR (integer)
- CURLOPT_SSL_CIPHER_LIST (integer)
- CURLOPT_BINARYTRANSFER (integer)
- CURLCLOSEPOLICY_LEAST_RECENTLY_USED (integer)
- CURLCLOSEPOLICY_LEAST_TRAFFIC (integer)
- CURLCLOSEPOLICY_SLOWEST (integer)
- CURLCLOSEPOLICY_CALLBACK (integer)
- CURLCLOSEPOLICY_OLDEST (integer)
- CURLINFO_EFFECTIVE_URL (integer)
- CURLINFO_HTTP_CODE (integer)
- CURLINFO_HEADER_SIZE (integer)
- CURLINFO_REQUEST_SIZE (integer)
- CURLINFO_TOTAL_TIME (integer)
- CURLINFO_NAMELOOKUP_TIME (integer)
- CURLINFO_CONNECT_TIME (integer)
- CURLINFO_PRETRANSFER_TIME (integer)
- CURLINFO_SIZE_UPLOAD (integer)
- CURLINFO_SIZE_DOWNLOAD (integer)
- CURLINFO_SPEED_DOWNLOAD (integer)
- CURLINFO_SPEED_UPLOAD (integer)
- CURLINFO_FILETIME (integer)
- CURLINFO_SSL_VERIFYRESULT (integer)
- CURLINFO_CONTENT_LENGTH_DOWNLOAD (integer)
- CURLINFO_CONTENT_LENGTH_UPLOAD (integer)
- CURLE_OK (integer)
- CURLE_UNSUPPORTED_PROTOCOL (integer)
- CURLE_FAILED_INIT (integer)
- CURLE_URL_MALFORMAT (integer)
- CURLE_URL_MALFORMAT_USER (integer)
- CURLE_COULDNT_RESOLVE_PROXY (integer)
- CURLE_COULDNT_RESOLVE_HOST (integer)
- CURLE_COULDNT_CONNECT (integer)
- CURLE_FTP_WEIRD_SERVER_REPLY (integer)
- CURLE_FTP_ACCESS_DENIED (integer)
- CURLE_FTP_USER_PASSWORD_INCORRECT (integer)
- CURLE_FTP_WEIRD_PASS_REPLY (integer)
- CURLE_FTP_WEIRD_USER_REPLY (integer)
- CURLE_FTP_WEIRD_PASV_REPLY (integer)
- CURLE_FTP_WEIRD_227_FORMAT (integer)
- CURLE_FTP_CANT_GET_HOST (integer)
- CURLE_FTP_CANT_RECONNECT (integer)
- CURLE_FTP_COULDNT_SET_BINARY (integer)
- CURLE_PARTIAL_FILE (integer)
- CURLE_FTP_COULDNT_RETR_FILE (integer)
- CURLE_FTP_WRITE_ERROR (integer)
- CURLE_FTP_QUOTE_ERROR (integer)
- CURLE_HTTP_NOT_FOUND (integer)
- CURLE_WRITE_ERROR (integer)
- CURLE_MALFORMAT_USER (integer)
- CURLE_FTP_COULDNT_STOR_FILE (integer)
- CURLE_READ_ERROR (integer)
- CURLE_OUT_OF_MEMORY (integer)
- CURLE_OPERATION_TIMEOUTED (integer)
- CURLE_FTP_COULDNT_SET_ASCII (integer)
- CURLE_FTP_PORT_FAILED (integer)
- CURLE_FTP_COULDNT_USE_REST (integer)
- CURLE_FTP_COULDNT_GET_SIZE (integer)
- CURLE_HTTP_RANGE_ERROR (integer)
- CURLE_HTTP_POST_ERROR (integer)
- CURLE_SSL_CONNECT_ERROR (integer)
- CURLE_FTP_BAD_DOWNLOAD_RESUME (integer)
- CURLE_FILE_COULDNT_READ_FILE (integer)
- CURLE_LDAP_CANNOT_BIND (integer)
- CURLE_LDAP_SEARCH_FAILED (integer)
- CURLE_LIBRARY_NOT_FOUND (integer)
- CURLE_FUNCTION_NOT_FOUND (integer)
- CURLE_ABORTED_BY_CALLBACK (integer)
- CURLE_BAD_FUNCTION_ARGUMENT (integer)
- CURLE_BAD_CALLING_ORDER (integer)
- CURLE_HTTP_PORT_FAILED (integer)
- CURLE_BAD_PASSWORD_ENTERED (integer)
- CURLE_TOO_MANY_REDIRECTS (integer)
- CURLE_UNKNOWN_TELNET_OPTION (integer)
- CURLE_TELNET_OPTION_SYNTAX (integer)
- CURLE_OBSOLETE (integer)
- CURLE_SSL_PEER_CERTIFICATE (integer)
Examples
Once you've compiled PHP with CURL support, you can begin using the CURL functions. The basic idea behind the CURL functions is that you initialize a CURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close(). Here is an example that uses the CURL functions to fetch the example.com homepage into a file:
- Table of Contents
- curl_close -- Close a CURL session
- curl_errno -- Return the last error number
- curl_error -- Return a string containing the last error for the current session
- curl_exec -- Perform a CURL session
- curl_getinfo -- Get information regarding a specific transfer
- curl_init -- Initialize a CURL session
- curl_multi_add_handle -- Add a normal cURL handle to a cURL multi handle
- curl_multi_close -- Close a set of cURL handles
- curl_multi_exec -- Run the sub-connections of the current cURL handle
- curl_multi_getcontent -- Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
- curl_multi_info_read -- Get information about the current transfers
- curl_multi_init -- Returns a new cURL multi handle
- curl_multi_remove_handle -- Remove a multi handle from a set of cURL handles
- curl_multi_select -- Get all the sockets associated with the cURL extension, which can then be "selected"
- curl_setopt -- Set an option for a CURL transfer
- curl_version -- Return the current CURL version