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