PHP: CURL, Client URL Library Functions - Manual
PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previouscrack_opendictcurl_closenext
Last updated: Tue, 09 Jul 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | Finnish | French | German | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

XII. CURL, Client URL Library Functions

Bevezet�s

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.

K�vetelm�nyek

In order to use the CURL functions you need to install the 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.

Telep�t�s

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.

El�re defini�lt �lland�k

Az itt list�zott �lland�kat ez a kiterjeszt�s defini�lja, �s csak akkor el�rhet�ek, ha az adott kiterjeszt�s be van ford�tva a PHP-be, vagy dinamikusan bet�lt�tt.

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)

P�ld�k

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

P�lda 1. Using PHP's CURL module to fetch the example.com homepage

<?php

$ch = curl_init ("http://www.example.com/");
$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);
?>

Tartalom
curl_close -- Close a CURL session
curl_errno -- Return an integer containing 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_setopt -- Set an option for a CURL transfer
curl_version -- Return the current CURL version
User Contributed Notes
CURL, Client URL Library Functions
add a note about notes

24-Aug-2000 11:01

For those of you looking for a win32 solution to get CURL to work, check
out  nice downloads set
up for you to enable the CURL functions along with a number of others.
(helped me out tremendously.)


09-Oct-2000 02:23

CURL seems to only allow you to download a remote file to a local file,
then process it after the fact.  It does not seem to allow for working on
a file while it is being downloaded.


05-Nov-2000 12:42

To return the output into a variable set CURLOPT_RETURNTRANSFER to 1, so
saith the php-dev list.


16-Dec-2000 01:31

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

dl("php_curl.dll");

if it says something like: can't use or load library files (or missing
dllz), 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 ectually 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 for by this DLL. This technique actually works for each dll :-)
Look mommy i'm being a nerd :-)

Good luck,
Emile


17-Jan-2001 08: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;
}


09-Feb-2001 09:57

There are undocumentated functions yet:

string curl_getinfo(int ch, int opt)
   Get information regarding a specific transfer

supported options:
CURLINFO_EFFECTIVE_URL
CURLINFO_HTTP_CODE
CURLINFO_HEADER_SIZE
CURLINFO_REQUEST_SIZE
CURLINFO_TOTAL_TIME
CURLINFO_NAMELOOKUP_TIME
CURLINFO_CONNECT_TIME
CURLINFO_PRETRANSFER_TIME
CURLINFO_SIZE_UPLOAD
CURLINFO_SIZE_DOWNLOAD
CURLINFO_SPEED_DOWNLOAD
CURLINFO_SPEED_UPLOAD


09-Feb-2001 10: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


27-May-2001 05: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)


22-Sep-2001 10:22

The PHP 4.0.6 code contains some functions that are not documented on this page:

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.

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

curl_errno(int ch) - Returns an integer containing the last error number

Here is some output from curl_getinfo():

Array
(
[url] =>
> [http_code] => 200
[header_size] => 261
[request_size] => 155
[filetime] => 0
[total_time] => 0.7408
[namelookup_time] => 0.031381
[connect_time] => 0.154251
[pretransfer_time] => 0.154589
[size_upload] => 0
[size_download] => 5612
[speed_download] => 3133.7556977376
[speed_upload] => 0
)


06-Jan-2002 01: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
msdn.microsoft.com 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.


31-Jan-2002 01:47

in addition to the files named above, you should also add the file
libeay32.dll to your PHP directory. The file can be found in the dlls
directory


25-May-2002 06:30

NOTE:
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.


22-Jun-2002 12:46

For an exaplanation of those Predefined Constants listed above see the
following URL: 

 


02-Jul-2002 03: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

loconet at hotmail dot com
05-Jul-2002 08:33

For those of you having problems loading php_curl.dll using Apache+PHP on
Win32, try copying  ssleay32.dll and libeay32.dll from the php/dlls
directory to your php root directory.

add a note about notes
previouscrack_opendictcurl_closenext
Last updated: Tue, 09 Jul 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Thu Jul 11 00:19:09 2002 CEST