downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Thu, 21 Aug 2003

XII. CURL, Client URL Library Functions


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.


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


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 SYSTEM32 folder of your Windows machine. (Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM32)

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_URL (integer)









CURLOPT_POST (integer)






CURLOPT_PUT (integer)

CURLOPT_MUTE (integer)





































































CURLE_OK (integer)





















































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 homepage into a file:

Example 1. Using PHP's CURL module to fetch the homepage


$ch = curl_init ("");
$fp = fopen ("example_homepage.txt", "w");

curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);

curl_exec ($ch);
curl_close ($ch);
fclose ($fp);

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

add a note add a note User Contributed Notes
CURL, Client URL Library Functions
rodneywa at yahoo dot com
24-Oct-2003 03:47
Here's how I was able to post arrays using Curl:


$submit_url = "";
$formvars["feild1"] = "feild1";

for ($i=0;$i<sizeof($array_of_vars);$i++)
    $formvars["array_of_vars[$i]"] = $dna[$i];

// init curl handle
$ch = curl_init($submit_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);

// perform post
echo $rr;
da at canaan dot co dot il
08-Oct-2003 09:42
i had problems with receiving all of the data that was replied to me about a transaction, atlast after way too much time spent i found out that all i missed was:
curl_setopt($ch, CURLOPT_HEADER, 1);

hope it helped anyone
alwong at 123infosys dot com
21-Sep-2003 09:35
The following scripts show how to post form to a web server via https.  I modified rodrigo posting because it did not work for me on IIS5.
= ';
    $params = "name=your_name&";
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https

    $result=curl_exec ($ch);
    curl_close ($ch);

    echo("Results: <br>".$result);
rodrigo dot nakahodo at dhweb dot com dot br
19-Sep-2003 02:57
The script shows how to get a simple response of a SSL Server. Enjoy it!



$_url '';
$_VAR001 'nono';
$_VAR002 'nonono';

$params "VAR001=$_VAR001&VAR002=$_VAR002&";

$ch curl_init();
$result=curl_exec ($ch);
curl_close ($ch);

"Output: ".$result);

helvecio_oliveira at yahoo dot com dot br
21-Aug-2003 11:12
CURL install steps in Mandrake 9.1:
cp -r /usr/src/php-devel/extensions/curl /tmp/curl
cd /tmp/curl
make install
echo "extension =" > /etc/php/90_curl.ini

Restart apache web server.
========================================================== is in:

look in phpinfo, the string:

Needs these  tools:

Some docs:
16-Jul-2003 05:01
Thanks to one of the posts above for pointing me in the right direction. If you get errors when trying to use php_curl.dll that it cannot be found and you have checked you have all the other dll's list above. If you used the installer package and copied the php_curl.dll from the binaries over because it is not included you also need to copy all the other extenstions dlls over and overwrite the others - then bingo it works!

PS If you are installing on W2003 don't forget to allow the php.exe and other dlls to work in the new server extentsions on the iis tree.
eric at imap dot ch
07-Jul-2003 11:38
I managed to use curl to retrieve information from severs on ports other than 80 or 443 (for https) on some installations but not on all.
If you get an "CURLE_COULDNT_CONNECT /* 7 */" error, try adding the port : (for example)
curl_setopt($ch, CURLOPT_PORT, $_SERVER['SERVER_PORT']);
php at mechintosh dot com
03-Jul-2003 10:51
IMHO the example code is somewhat confusing; the fopen() and fclose() are not needed at all, AFAICT.

By default the resulting data/page of the external server is just printed out. To assign the result of the curl session to a variable, one can use
- output buffering (see ob_start(), ob_get_contents())
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
With the latter, the result of curl_exec is the actual data instead of a boolean.
28-Jun-2003 04:01
It should be noted that to use curl with https you must make sure that (lib)curl was configured with the --with-ssl=/path-to/(open)ssl AND that apache is running with SSL support (mod_ssl). If both of these aren't done you will get an error saying 'libcurl was built with SSL disabled, https: not supported!' This may seen obvious to some but thought it warranted a note.
www dot php dot net at roderick dot triple-it dot nl
16-Jun-2003 04:31
Using CURL && Openssl

If you get an error like this:
Error 35: SSL: error:14090086:lib(20):func(144):reason(134)

It means that the verifcation path is unsuccessful (I guess).

Anyway, a somewhat unsecure workaround is:
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);

Hope this helpes somebody..
(I was already installing egd, prngd and SUNWski stuff)

Kind Regards,

Roderick Groesbeek
yc at_ laposte dot_ net
21-May-2003 06:46
Do not try to send mutliple HTTP requests with one curl session, because this seems to cause curl to hang randomly, though the requests are executed without problem. So it seems much more efficient to do a curl_close and curl_init between each request.
hope this helps
10-Apr-2003 03:44
function curl_string ($url,$user_agent,$proxy){

        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_PROXY, $proxy);
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
        curl_setopt ($ch, CURLOPT_HEADER, 1);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
        $result = curl_exec ($ch);
        return $result;

$url_page = "";
$user_agent = "Mozilla/4.0";
$proxy = "";
$string = curl_string($url_page,$user_agent,$proxy);
echo $string;

this could hep someone
thomas at NOSPAM dot gutschke dot com
07-Apr-2003 07:00
Here's a little code snippet if you need the last-modified-time for a remote file as filemtime() does not work on those. :)

$modified = "";
function read_header($ch, $header)
    global $modified;
    $length = strlen($header);
    if(strstr($header, "Last-Modified:"))
        $modified = substr($header, 15);
    return $length;

function last_mod($remote_file)
    global $modified;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remote_file);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');

    $headers = curl_exec ($ch);
    curl_close ($ch);
    return $modified;

use something like
    echo strtotime(last_mod(""));
to get the time as an Unix timestamp or
    echo last_mod("");
to get the gmdate()-like time.
dweingart at pobox dot com
03-Apr-2003 06:08
If you want to Curl to follow redirects and you would also like Curl to echo back any cookies that are set in the process, use this:

curl_setopt($ch, CURLOPT_COOKIEJAR, '-');

'-' means stdout

diana dot castillo at nvtechnologies dot com
26-Mar-2003 02:30
if you need to install curl on a windows system, this has the best explanation:
bharris at spro dot net
26-Feb-2003 08:58
For Win2000: To get the 4.3.1 curl dll to work with https you now need to download the latest win32 curl library from and snag the ca-bundle.crt file from the lib directory.  Place this somewhere handy on your webserver.

Then in your PHP script, add the following setopt line to the rest of your curl_setopt commands:

curl_setopt($ch, CURLOPT_CAFILE, 'C:\pathto\ca-bundle.crt')

This worked for me and allowed me to discontinue using the CURLOPT_SSL_VERIFYPEER set to zero hack.
stain at dxml dot org
13-Feb-2003 08:33
php 4.3.0 + curl 7.10.3

in my php code i do something like:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, '');

curl_exec ($ch);

if (curl_error($ch))
        printf("Error %s: %s", curl_errno($ch), curl_error($ch));

curl_close ($ch);

and i retrieve an error like:

Error 35: SSL: error:xxxxxxxx:lib(xx):func(xxx):reason(xxx)

curl error 35 is CURLE_SSL_CONNECT_ERROR 

from SSLCERTS file in curl source directory:
"Starting in 7.10, libcurl performs peer SSL certificate verification by
If the remote server uses a self-signed certificate, or if you don't install
curl's CA cert bundle or if it uses a certificate signed by a CA that isn't
included in the bundle, then you need to do one of the following:
 1. Tell libcurl to *not* verify the peer. With libcurl you disable with with
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    With the curl command tool, you disable this with -k/--insecure.

(i suggest to read all the info provided with curl distribution)

so, in php, you need do the following:

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

 i would be sure my connection don't crash, but since i didn't find any other documentation available, i add also the following:

curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);

from php manual:
"CURLOPT_SSL_VERIFYHOST: Pass a long if CURL should verify the Common name of the peer certificate in the SSL handshake. A value of 1 denotes that we should check for the existence of the common name, a value of 2 denotes that we should make sure it matches the provided hostname. "


bye, stain.
jobob AT
23-Jan-2003 02:18
There is a site which uses curl to do a lot of automated link checking in a script is <a href=">affiliate source</a> which seems to be pretty interesting in the script they use. Here is a modified version.

function GetCurlPage ($pageSpec) {
    $agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
    $ref = "";

       $ch = curl_init($pageSpec);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_REFERER, $ref);

       $tmp = curl_exec ($ch);
       curl_close ($ch);
       $ret = $tmp;
       $ret = @implode('', @file ($pageSpec));
    return $ret;

This check if CURL is installed first before attempting to call it, this is for those of you who are not sure if it is. (shared hosting etc.)

retro at nospam dot enx dot org
12-Jan-2003 09:42
When using cURL to do posts, make sure you actually specify the CURLOPT_POSTFIELDS option. Not doing so seems to make PHP core (and apache as well if you're running it through there).
laurinus at blubb dot at
03-Jan-2003 02:55
Halleluja - it took me 2 Days to find this...

David Withnall wrote:
There are 2 windows install versions of php available on the php site. the first is the executable install. the second is the zipped binary.
If you install php using the executable install it does not install php_curl.dll If you then copy this dll out of the zipped binary and put it into the extensions directory it will fail. This is because the dlls in the zipped binary are not in sync with the executable install.
20-Dec-2002 12:03
mwartman, perhaps you need to use https:
$ch = curl_init ("");
asi at neo dot ee
13-Dec-2002 11:28
If anybody has problems with getting new curl working with older version of php then ...

I got stuck with installing curl-7.10.2+php 4.0.6 or better said configuring PHP with mentioned version of curl.

After some hopeless attempts to configure curl support into PHP I saw an error message:

"checking for cURL greater than or equal to 7.8... ./configure: line 11725: test: 070a02: integer expression expected"

after which configure said: "configure: error: cURL version 7.8 or later is required to compile php with cURL support"

On the mentioned line "test" is used to compare verion from curl-sonfig with preset value 70800 but ... test expects both values to be integer. From verson 7.8 curl outputs its version in hex...

I did'nt had the time to think about fixing this - I just deleted lines responsible and got curl working :)
mwartman at betterbuilt dot com
02-Nov-2002 06:57
a note on installing apache+php+ssl+curl+mysql. this is cake. if you go to and download their free tech platform, it is a nice easy little web based installer that installs and configures apache, mysql, posgresql, ssl, php, phpmyadmin, and a few other tools i can't think of right now. to get it working with curl support, all you have to do is edit the c:\Program Files\nusphere\apache\php\php.ini, and remove the ; from in front of the php_curl extension. it's all up and running in minutes. =]
jerome at blaster dot dyndns dot org
10-Oct-2002 09:23
I wanted to import an https Web pages thanks to fopen("https://xxx", "r") or fsockopen but as far as I read, it was only possible with cURL extension (to get these functions worked with https, we have to wait the PHP 4.3 release, TBC) , so I installed it and it worked. It is quite easy, but I spend to much time on it:
First of all, here is the most important: WE DON'T NEED to compile anything to get curl worked. All the extensions that we need are already in the PHP 4.2.3 zip. So here are the instructions and links to get Apache + PHP + CURL work properly:

=> Apache Web server with Openssl included:

=> PHP 4.2.3 for Windows:

=> Optional: Openssl zip to get libeay32.dll and ssleay32.dll (I didn't need them because I used those stored in PHP zip) OpenSSL-0.9.6d-win32_RAR3_Archive_.rar

=> Unzip Apache in Program Files folder(there should be no problem with the blank in the path):

=> Follow those instructions: . I didn't exactly do what they wrote but it helped a lot. I created the test certificat thanks to this link in order to test my Apache server with SSL.

=> Configure httpd (easy to find on the Web) and test Apache before going on, it avoids problems.
   I just changed the Port from 80 (http) to 443 (https) in httpd, I only wanted to perform https.
   I didn't try to get the both working together.

=> Unzip PHP in C:\Php path (don't install it in Program Files folder, you would have troubles because of the blank this time...)

=> Configure PHP :
   WARNING: php.ini MUST be stored into the c:\winnt folder and the dlls must be stored into the c:\winnt\system32 folder.

=> Test it with Apache and phpinfo() function.

To add cURL extension, YOU DON'T NEED to compile anything.
All the extensions that you need are in the C:\php\extensions folder. To add cURL or any other extensions:

=> Have the libeay32.dll and ssleay32.dll in the system32 folder (already done if you installed PHP)

=> Copy all dlls from php/dll/ folder to winnt/system32/ (libeay32.dll and ssleay32.dll are already there if you followed
   the previous steps)

=> Remove the ";" in front of extension=php_curl.dll in php.ini.

Those instructions come from:

And it should work, just test it with curl functions into your php script.
steve at bakeacake dot com
17-Sep-2002 11:41
When opening a webpage with cURL, returns are a "\n" when you open a webpage with file the returns are a "\r".
26-Aug-2002 07:31
[Editors note]
Make sure that the supplied libeay32.dll and ssleay32.dll
(in the dlls-Folder) are on a path where PHP can find them.

A tip for Windows users (and possibly others): if you really can't get the cURL library (php_curl.dll) to work (like me), but you can get the precompiled command line utility (curl.exe) that's available from the cURL site to work, you can use PHP's system command execution facilities, like the backtick operator, to invoke cURL and obtain its output.
hamannDOTw at tDOTonline dot de
02-Jul-2002 11:18
Using the customrequest for a complete post is wrong. Libcurl will add a partial url, the http version and the standard headers after the post data - while this works with a non-persistent connection and an apache web server, it may fail under different conditions
smclean at scoreinfo dot tv
22-Jun-2002 08:46
For an exaplanation of those Predefined Constants listed above see the following URL:
brian dot m at bizatomic dot com
26-May-2002 02:30
There is a bug in cURL version 7.9.4 that can cause problems form posts.

I just spent a couple hours pulling my hair out over this.

I just upgraded to 7.9.7 and everything seems to work fine.

Here's a link to info on the cURL site.
orangevwbus at yahoo dot com
06-Jan-2002 11:12
In win32 (Windows 2000), I couldn't get apache to start with PHP/curl support 'cause it said it couldn't find the php_curl.dll.  Well it was there.
MSVCRT.dll was there as well, but after I ran the dll through "Depends", there's a new DLL for the Microsoft .NET stuff. It's called msvcr70.dll, and without it, php_curl.dll won't load.
I didn't want to load .NET framework on my machine (which suggested)
I did a google search and found a downloadable copy of the file and threw it in my %system%\system32 folder and all the lights and whistles came on.
jeff at vertexdev dot com
23-Sep-2001 06:22
<p>The PHP 4.0.6 code contains some functions that are not documented on this page:</p>

<p>curl_getinfo(int ch) - Returns an associative array with detailed information about the last transfer, including the actual URL fetched, the HTTP response code, and the time taken to perform each phase of the transfer.</p>

<p>curl_error(int ch) - Returns a descriptive error string containing the last error code for the session.</p>

<p>curl_errno(int ch) - Returns an integer containing the last error number</p>

<p>Here is some output from curl_getinfo():</p>

    [url] =><br><br>
    [http_code] => 200<br>
    [header_size] => 261<br>
    [request_size] => 155<br>
    [filetime] => 0<br>
    [total_time] => 0.7408<br>
    [namelookup_time] => 0.031381<br>
    [connect_time] => 0.154251<br>
    [pretransfer_time] => 0.154589<br>
    [size_upload] => 0<br>
    [size_download] => 5612<br>
    [speed_download] => 3133.7556977376<br>
    [speed_upload] => 0<br>
marco dot mcc at inwind dot it
28-May-2001 01:50
If you want to write your entire HTTP request without use any other CURL functions like CURLOPT_POST specify it within a curl_setopt ($ch,CURLOPT_CUSTOMREQUEST , $req) line;
Where $req looks like (let me imagine a POST request...):

POST /destination/script HTTP/1.1
Content-length: xxx
Content-type: text/xml
host: yourhost
accept: */*
accept-encoding: gzip, deflate
accept-language: en-us
connection: close; Keep-Alive
your POST data

CURLOPT_CUSTOMREQUEST is not documented?!? but it's useful and amazing!

marcomcc (Roma)
tom dot anheyer at berlinonline dot de
09-Feb-2001 08:01
string curl_error(int ch)
   Return a string contain the last error for the current session
int curl_errno(int ch)
   Return an integer containing the last error number

the error codes are defined as constants in ../ext/curl/curl.c
dan dot polansky at seznam dot cz
18-Jan-2001 06:31
I used to download www pages to my script and one of the pages was different in MS explorer and different, when I downloaded it. Namely, information, I was really interested in was missing. That was because the server on the other bank of the river was looking at who is downloading the page. Everything got fixed when I pretended I was MSIE. It is done with curl. Here is a function, that you may use in similar situation

function download_pretending($url,$user_agent) {
   $ch = curl_init();
   curl_setopt ($ch, CURLOPT_URL, $url);
   curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
   curl_setopt ($ch, CURLOPT_HEADER, 0);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec ($ch);
   curl_close ($ch);
   return $result;
emilebosch at hotmail dot com
16-Dec-2000 11:31
[Editor's Note]
Please note that you can load the php_curl.dll extension by modifying your php.ini file to include the correct extension_dir setting and then adding or uncommenting the extension line for php_curl.dll, you do not need to copy the extension to your windows directory.

For all you people that can't get CURL to work on Windows load the Dynamic Library by using:


if it says something like: can't use or load library files (or missing dll), it's missing some DLL's like
php4ts.dll,SSLEAY32.dll,php_curl.dll,MSVCRT.dll locate these DLL's in your PHP binary and put them in your windows SYSTEM directory, i actually found this out by editing the php_curl.dll file and looked for the DLL files its need (by searching for .dll) and just made sure that those files where accessible by this DLL.

Good luck,
macmaster at pobox dot com
05-Nov-2000 10:42
To return the output into a variable set CURLOPT_RETURNTRANSFER to 1, so saith the php-dev list.

 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST