CURL, Client URL Library functionsCURL
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.
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.
To use PHP's CURL support you must also compile PHP
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.
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_exec() and
then you finish off your session using the curl_close().
Here is an example that uses the CURL functions to fetch the PHP homepage into
a file:
Using PHP's CURL module to fetch the PHP homepage
<?php
$ch = curl_init ("http://www.php.net/");
$fp = fopen ("php_homepage.txt", "w");
curl_setopt ($ch, CURLOPT_INFILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
fclose ($fp);
?>
curl_initInitialize a CURL sessionDescriptionint
curl_initstring
url
The curl_init will initialize a new session
and return a CURL handle for use with the curl_setopt,
curl_exec, and curl_close
functions. If the optional url parameter is supplied
then the CURLOPT_URL option will be set to the value of the parameter.
You can manually set this using the curl_setopt
function.
Initializing a new CURL session and fetching a webpage
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.zend.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
See also: curl_close, curl_setoptcurl_setoptSet an option for a CURL transferDescriptionbool
curl_setoptint
chstring
optionmixed
value
The curl_setopt function will set options for a CURL
session identified by the ch parameter. The
option parameter is the option you want to set,
and the value is the value of the option
given by the option.
The value should be a long for the following
options (specified in the option parameter):
CURLOPT_INFILESIZE: When you are uploading
a file to a remote site, this option should be used to tell PHP
what the expected size of the infile will be.
CURLOPT_VERBOSE: Set this option to a non-zero
value if you want CURL to report everything that is happening.
CURLOPT_HEADER: Set this option to a non-zero
value if you want the header to be included in the output.
CURLOPT_NOPROGRESS: Set this option to a non-zero
value if you don't want PHP to display a progress meter for CURL transfers
PHP automatically sets this option to a non-zero parameter, this should
only be changed for debugging purposes.
CURLOPT_NOBODY: Set this option to a non-zero value
if you don't want the body included with the output.
CURLOPT_FAILONERROR: Set this option to a non-zero
value if you want PHP to fail silently if the HTTP code returned is greater
than 300. The default behaviour is to return the page normally, ignoring
the code.
CURLOPT_UPLOAD: Set this option to a non-zero value
if you want PHP to prepare for an upload.
CURLOPT_POST: Set this option to a non-zero value
if you want PHP to do a regular HTTP POST. This POST is a normal
application/x-www-from-urlencoded kind, most commonly used by HTML forms.
CURLOPT_FTPLISTONLY: Set this option to a non-zero
value and PHP will just list the names of an FTP directory.
CURLOPT_FTPAPPEND: Set this option to a non-zero
value and PHP will append to the remote file instead of overwriting it.
CURLOPT_NETRC: Set this option to a non-zero value
and PHP will scan your ~./netrc file to find your username and password
for the remote site that you're establishing a connection with.
CURLOPT_FOLLOWLOCATION: Set this option to a
non-zero value to follow any "Location: " header that the server sends as
a part of the HTTP header (note this is recursive, PHP will follow as many
"Location: " headers that it is sent.)
CURLOPT_PUT: Set this option a non-zero value to
HTTP PUT a file. The file to PUT must be set with the CURLOPT_INFILE and
CURLOPT_INFILESIZE.
CURLOPT_MUTE: Set this option to a non-zero value
and PHP will be completely silent with regards to the CURL functions.
CURLOPT_TIMEOUT: Pass a long as a parameter that
contains the maximum time, in seconds, that you'll allow the curl functions
to take.
CURLOPT_LOW_SPEED_LIMIT: Pass a long as a parameter
that contains the transfer speed in bytes per second that the transfer should
be below during CURLOPT_LOW_SPEED_TIME seconds for PHP to consider it too
slow and abort.
CURLOPT_LOW_SPEED_TIME: Pass a long as a parameter
that contains the time in seconds that the transfer should be below the
CURLOPT_LOW_SPEED_LIMIT for PHP to consider it too slow and abort.
CURLOPT_RESUME_FROM: Pass a long as a parameter that
contains the offset, in bytes, that you want the transfer to start from.
CURLOPT_SSLVERSION: Pass a long as a parameter that
contains the SSL version (2 or 3) to use. By default PHP will try and
determine this by itself, although, in some cases you must set this
manually.
CURLOPT_TIMECONDITION: Pass a long as a parameter
that defines how the CURLOPT_TIMEVALUE is treated. You can set this
parameter to TIMECOND_IFMODSINCE or TIMECOND_ISUNMODSINCE. This is a
HTTP-only feature.
CURLOPT_TIMEVALUE: Pass a long as a parameter that
is the time in seconds since January 1st, 1970. The time will be used as
specified by the CURLOPT_TIMEVALUE option, or by default the
TIMECOND_IFMODSINCE will be used.
The value parameter should be a string for the following
values of the option parameter:
CURLOPT_URL: This is the URL that you want PHP to
fetch. You can also set this option when initializing a session with the
curl_init function.
CURLOPT_USERPWD: Pass a string formatted in the
[username]:[password] manner, for PHP to use for the connection.
connection.
CURLOPT_PROXYUSERPWD: Pass a string formatted in
the [username]:[password] format for connection to the HTTP proxy.
CURLOPT_RANGE: Pass the specified range you want.
It should be in the "X-Y" format, where X or Y may be left out. The HTTP
transfers also support several intervals, seperated with commas as in
X-Y,N-M.
CURLOPT_POSTFIELDS: Pass a string containing the
full data to post in an HTTP "POST" operation.
CURLOPT_REFERER: Pass a string containing the
"referer" header to be used in an HTTP request.
CURLOPT_USERAGENT: Pass a string containing the
"user-agent" header to be used in an HTTP request.
CURLOPT_FTPPORT: Pass a string containing the which
will be used to get the IP address to use for the ftp "PORT" instruction.
The POST instruction tells the remote server to connect to our specified
IP address. The string may be a plain IP address, a hostname, a network
interface name (under UNIX), or just a plain '-' to use the systems default
IP address.
CURLOPT_COOKIE: Pass a string containing the
content of the cookie to be set in the HTTP header.
CURLOPT_SSLCERT: Pass a string containing the
filename of PEM formatted certificate.
CURLOPT_SSLCERTPASSWD: Pass a string containing
the password required to use the CURLOPT_SSLCERT certificate.
CURLOPT_COOKIEFILE: Pass a string containing
the name of the file containing the cookiee data. The cookie file can be
in Netscape format, or just plain HTTP-style headers dumped into a file.
CURLOPT_CUSTOMREQUEST: Pass a string to be used
instead of GET or HEAD when doing an HTTP request. This is useful for
doing DELETE or another, more obscure, HTTP request.
Don't do this without making sure your server supports the command
first.
The following options expect a file descriptor that is obtained by using the
fopen function:
CURLOPT_FILE: The file where the output of your
transfer should be placed, the default is STDOUT.
CURLOPT_INFILE: The file where the input of your
transfer comes from.
CURLOPT_WRITEHEADER: The file to write the header
part of the output into.
CURLOPT_STDERR: The file to write errors to instead
of stderr.
curl_execPerform a CURL sessionDescriptionbool
curl_execint
ch
This function is should be called after you initialize a CURL session and all the
options for the session are set. Its purpose is simply to execute the predefined
CURL session (given by the ch).
curl_closeClose a CURL sessionDescriptionvoid
curl_closeint
ch
This functions closes a CURL session and frees all ressources.
The CURL handle, ch, is also deleted.
curl_versionReturn the current CURL versionDescriptionstring
curl_version
The curl_version function returns a string containing the
current CURL version.