|
|
XII. Funzioni CURL, Client URL LibraryIntroduzione
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.
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 |
|
[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.
|
|
|
| |