PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previouscrack_opendictcurl_closenext
Last updated: Tue, 03 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Hungarian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

XII. Funzioni CURL, Client URL Library

Introduzione

PHP supporta libcurl, una libreria creata da Daniel Stenberg, che permette di collegarsi e comunicare con parecchi tipi di server e con parecchi tipi di protocolli. Libcurl al momento supporta i protocolli http, https, ftp, gopher, telnet, dict, file, e ldap. libcurl supporta anche i certificati HTTPS, HTTP POST, HTTP PUT, l'upload via FTP (questo pu� essere ottenuto anche con l'estensione ftp di PHP), upload attraverso una form HTTP, proxy, cookie e autenticazione con utente e password.

Queste funzioni sono state aggiunte nel PHP 4.0.2.

Requisiti

Per utilizzare le funzioni CURL occorre installare il pacchetto . PHP richiede che si usi CURL 7.0.2-beta o successivi. PHP non funzioner� con alcuna versione di CURL antecedente alla 7.0.2-beta.

Istallazione

Al fine di utilizzare il supporto CURL occorre anche compilare PHP con --with-curl[=DIR] dove DIR � il percorso della directory che contiene le directory lib e include. Nella directory "include" ci dovrebbe essere una cartella chiamata "curl" che dovrebbe contenere i file easy.h e curl.h. Ci dovrebbe essere un file chiamato "libcurl.a" nella directory "lib".

Costanti Predefinite

Queste costanti sono definite da questa estensione e sono disponibili solo se l'estensione � stata compilata nel PHP o se � stata caricata dinamicamente a 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)

Esempi

Una volta compilato PHP con il supporto CURL, si pu� iniziare a usare le funzioni curl. L'idea di fondo che sta dietro le funzioni CURL �: si inizializza una sessione CURL usando curl_init(), si impostano le opzioni per il trasferimento tramite curl_exec() e quindi si termina la sessione usando curl_close(). Qui di seguito si trova un esempio che fa uso delle funzioni CURL per scaricare la homepage del sito example.com e metterla in un file:

Esempio 1. Usare il modulo CURL di PHP per scaricare la homepage di example.com

<?php

$ch = curl_init ("http://www.example.com/");
$fp = fopen ("homepage_example.txt", "w");

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

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

Sommario
curl_close -- Chiude una sessione CURL
curl_errno -- Restituisce un intero contenente il numero dell'ultimo errore
curl_error --  Restituisce una stringa contenente l'ultimo errore relativo alla sessione corrente
curl_exec -- Esegue una sessione CURL
curl_getinfo --  Ottiene informazioni relative a un determinato trasferimento
curl_init -- Inizializza una sessione CURL
curl_setopt -- Imposta una opzione per un trasferimento CURL
curl_version -- Restituisce la versione di CURL in uso
User Contributed Notes
Funzioni CURL, Client URL Library
add a note about notes
[email protected]
25-Aug-2000 12: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.)
[email protected]
09-Oct-2000 03: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.
[email protected]
05-Nov-2000 01:42

To return the output into a variable set CURLOPT_RETURNTRANSFER to 1, so saith the php-dev list.
[email protected]
16-Dec-2000 02: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

[email protected]
17-Jan-2001 09: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;
}

[email protected]
09-Feb-2001 10: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

[email protected]
09-Feb-2001 11: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

[email protected]
27-May-2001 06: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)

[email protected]
22-Sep-2001 11: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

)

[email protected]
06-Jan-2002 02: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.

[email protected]
31-Jan-2002 02: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
[email protected]
25-May-2002 07: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.

[email protected]
22-Jun-2002 01:46

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


[email protected]
02-Jul-2002 04: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 09: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.
snake @ bigfoot . com
12-Jul-2002 12:29

do not run the example snippet if you're using php 4.2.1 and libcurl 7.9.8 (or later versions i would assume); php.exe will crash even if you have the required DLLs in system32.

look at for some simple example code.

ibyte(at)SPAMISKILLEDnoxs.nl
26-Aug-2002 12: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.

add a note about notes
previouscrack_opendictcurl_closenext
Last updated: Tue, 03 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Fri Sep 6 12:35:50 2002 CEST