Manuale PHP
Indietro |  | Avanti

# XVIII. 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 in PHP 4.0.2.

# Requisiti

Per utilizzare le funzioni CURL occorre installare il pacchetto CURL. 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. Dalla versione 4.2.3 di PHP è necessario usare CURL versione 7.9.0 o successiva. Con PHP 4.3.0 occorre avere CURL 7.9.8 o successive. PHP 5.0.0 molto probabilmente richiederà CURL 7.10.5 o successiva.

# Installazione

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". A cominciare da PHP 4.3.0 si può configurare PHP all'uso di CURL per gli URL stream --with-curlwrappers.

> Nota agli utenti Win32: Per abilitare questo modulo in ambiente Windows, occorre copiare `libeay32.dll` e `ssleay32.dll` dalla cartella delle DLL del pacchetto PHP/Win32 nella cartella SYSTEM32 della propria macchina Windows. (Es: `C:\WINNT\SYSTEM32` o `C:\WINDOWS\SYSTEM`)

# 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_DNS_USE_GLOBAL_CACHE` (integer)

`CURLOPT_DNS_CACHE_TIMEOUT` (integer)

`CURLOPT_FTPSSLAUTH` (integer)

  Disponibile da PHP 5.1.0

`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_FTP_USE_EPSV` (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_CAPATH` (integer)

`CURLOPT_COOKIEJAR` (integer)

`CURLOPT_SSL_CIPHER_LIST` (integer)

`CURLOPT_BINARYTRANSFER` (integer)

`CURLOPT_NOSIGNAL` (integer)

`CURLOPT_PROXYTYPE` (integer)

`CURLOPT_BUFFERSIZE` (integer)

`CURLOPT_HTTPGET` (integer)

`CURLOPT_HTTP_VERSION` (integer)

`CURLOPT_SSLKEY` (integer)

`CURLOPT_SSLKEYTYPE` (integer)

`CURLOPT_SSLKEYPASSWD` (integer)

`CURLOPT_SSLENGINE` (integer)

`CURLOPT_SSLENGINE_DEFAULT` (integer)

`CURLOPT_SSLCERTTYPE` (integer)

`CURLOPT_CRLF` (integer)

`CURLOPT_ENCODING` (integer)

`CURLOPT_PROXYPORT` (integer)

`CURLOPT_UNRESTRICTED_AUTH` (integer)

`CURLOPT_FTP_USE_EPRT` (integer)

`CURLOPT_HTTP200ALIASES` (integer)

`CURLOPT_HTTPAUTH` (integer)

`CURLAUTH_BASIC` (integer)

`CURLAUTH_DIGEST` (integer)

`CURLAUTH_GSSNEGOTIATE` (integer)

`CURLAUTH_NTLM` (integer)

`CURLAUTH_ANY` (integer)

`CURLAUTH_ANYSAFE` (integer)

`CURLOPT_PROXYAUTH` (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)

`CURLINFO_STARTTRANSFER_TIME` (integer)

`CURLINFO_CONTENT_TYPE` (integer)

`CURLINFO_REDIRECT_TIME` (integer)

`CURLINFO_REDIRECT_COUNT` (integer)

`CURL_VERSION_IPV6` (integer)

`CURL_VERSION_KERBEROS4` (integer)

`CURL_VERSION_SSL` (integer)

`CURL_VERSION_LIBZ` (integer)

`CURLVERSION_NOW` (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)

`CURLE_GOT_NOTHING` (integer)

`CURLE_SSL_ENGINE_NOTFOUND` (integer)

`CURLE_SSL_ENGINE_SETFAILED` (integer)

`CURLE_SEND_ERROR` (integer)

`CURLE_RECV_ERROR` (integer)

`CURLE_SHARE_IN_USE` (integer)

`CURLE_SSL_CERTPROBLEM` (integer)

`CURLE_SSL_CIPHER` (integer)

`CURLE_SSL_CACERT` (integer)

`CURLE_BAD_CONTENT_ENCODING` (integer)

`CURLE_LDAP_INVALID_URL` (integer)

`CURLE_FILESIZE_EXCEEDED` (integer)

`CURLFTPAUTH_DEFAULT` (integer)

  Available since PHP 5.1.0

`CURLFTPAUTH_SSL` (integer)

  Available since PHP 5.1.0

`CURLFTPAUTH_TLS` (integer)

  Disponibile da PHP 5.1.0

`CURLE_FTP_SSL_FAILED` (integer)

`CURLPROXY_HTTP` (integer)

`CURLPROXY_SOCKS5` (integer)

`CURL_NETRC_OPTIONAL` (integer)

`CURL_NETRC_IGNORED` (integer)

`CURL_NETRC_REQUIRED` (integer)

`CURL_HTTP_VERSION_NONE` (integer)

`CURL_HTTP_VERSION_1_0` (integer)

`CURL_HTTP_VERSION_1_1` (integer)

`CURLM_CALL_MULTI_PERFORM` (integer)

`CURLM_OK` (integer)

`CURLM_BAD_HANDLE` (integer)

`CURLM_BAD_EASY_HANDLE` (integer)

`CURLM_OUT_OF_MEMORY` (integer)

`CURLM_INTERNAL_ERROR` (integer)

`CURLMSG_DONE` (integer)

# Esempi

Una volta compilato PHP con supporto CURL, si possono 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_setopt(), si esegue la sessione usando curl_exec() e quindi si termina la sessione con 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("example_homepage.txt", "w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); ?>`

Indietro | Partenza | Avanti
ctype_xdigit | Risali | curl_close