|
|
XII. CURL, Client URL Library FunctionsIntroductie
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.
Afhankelijkheden
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. From PHP version 4.2.3 you will atleast
need CURL version 7.9.0 or higher.
Installatie
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. Beginning
with PHP 4.3.0 you can configure PHP to use CURL for url streams
--with-curlwrappers.
Note to Win32 Users:
In order to enable this module on a Windows environment, you must copy
libeay32.dll and ssleay32.dll
from the DLL folder of the PHP/Win32 binary package to the SYSTEM32
folder of your windows machine. (Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM32)
Voorgedefineerde constanten
Deze constanten worden gedefineerd door deze extensie, en
zullen alleen beschikbaar zijn als de extensie met PHP is
meegecompileerd, of als deze dynamisch is geladen vanuit een script.
Voorbeelden
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_setopt(),
then you can execute the session with 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:
Voorbeeld 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);
?> |
|
User Contributed Notes CURL, Client URL Library Functions |
add a note |
macmaster at pobox dot com
05-Nov-2000 01:42 |
|
To return the output into a variable set CURLOPT_RETURNTRANSFER to 1, so
saith the php-dev list.
|
|
emilebosch at hotmail dot com
16-Dec-2000 02:31 |
|
[Editor's Note]
Please note that you can load the php_curl.dll
extension by modifying your php.ini file to include the correct
extension_dir setting and then adding or uncommenting the extension line
for php_curl.dll, you do not need to copy the extension to your windows
directory.
For all you 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 dll), 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
actually 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 by this DLL.
Good luck,
Emile
|
|
dan dot polansky at seznam dot cz
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;
}
|
|
tom dot anheyer at berlinonline dot de
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
|
|
marco dot mcc at inwind dot it
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)
|
|
jeff at vertexdev dot com
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
)
|
|
orangevwbus at yahoo dot com
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.
|
|
d dot kingma at ContentS dot nl
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
|
|
brian dot m at bizatomic dot com
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.
|
|
smclean at scoreinfo dot tv
22-Jun-2002 01:46 |
|
For an exaplanation of those Predefined Constants listed above see the
following URL:
|
|
hamannDOTw at tDOTonline dot de
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.
|
|
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.
|
|
steve at bakeacake dot com
17-Sep-2002 04:41 |
|
When opening a webpage with cURL, returns are a "\n" when you
open a webpage with file the returns are a "\r".
|
|
jerome at blaster dot dyndns dot org
10-Oct-2002 02:23 |
|
I wanted to import an https Web pages thanks to
fopen("https://xxx", "r") or fsockopen but as far as I
read, it was only possible with cURL extension (to get these functions
worked with https, we have to wait the PHP 4.3 release, TBC) , so I
installed it and it worked. It is quite easy, but I spend to much time on
it: First of all, here is the most important: WE DON'T NEED to compile
anything to get curl worked. All the extensions that we need are already
in the PHP 4.2.3 zip. So here are the instructions and links to get Apache
+ PHP + CURL work properly:
Downloads: => Apache Web server
with Openssl included:
Apache_1.3.26-Mod_SSL_2.8.9-OpenSSL_0.9.6d-WIN32.zip
=> PHP
4.2.3 for Windows:
=>
Optional: Openssl zip to get libeay32.dll and ssleay32.dll (I didn't need
them because I used those stored in PHP zip)
OpenSSL-0.9.6d-win32_RAR3_Archive_.rar
Installs: => Unzip
Apache in Program Files folder(there should be no problem with the blank
in the path):
=> Follow those instructions:
. I didn't exactly do what they wrote but it helped a lot. I created the
test certificat thanks to this link in order to test my Apache server with
SSL.
=> Configure httpd (easy to find on the Web) and test
Apache before going on, it avoids problems. I just changed the Port
from 80 (http) to 443 (https) in httpd, I only wanted to perform
https. I didn't try to get the both working together.
=>
Unzip PHP in C:\Php path (don't install it in Program Files folder, you
would have troubles because of the blank this time...)
=>
Configure PHP :
#install.windows.manual WARNING: php.ini MUST be stored into the
c:\winnt folder and the dlls must be stored into the c:\winnt\system32
folder.
=> Test it with Apache and phpinfo()
function.
cURL: To add cURL extension, YOU DON'T NEED to compile
anything. All the extensions that you need are in the C:\php\extensions
folder. To add cURL or any other extensions:
=> Have the
libeay32.dll and ssleay32.dll in the system32 folder (already done if you
installed PHP)
=> Copy all dlls from php/dll/ folder to
winnt/system32/ (libeay32.dll and ssleay32.dll are already there if you
followed the previous steps)
=> Remove the ";"
in front of extension=php_curl.dll in php.ini.
Those instructions
come from:
#install.windows.extensions
And it should work, just test it with
curl functions into your php script.
|
|
mwartman at betterbuilt dot com
01-Nov-2002 09:57 |
|
a note on installing apache+php+ssl+curl+mysql. this is cake. if you go to
nusphere.com and download their free tech platform, it is a nice easy
little web based installer that installs and configures apache, mysql,
posgresql, ssl, php, phpmyadmin, and a few other tools i can't think of
right now. to get it working with curl support, all you have to do is edit
the c:\Program Files\nusphere\apache\php\php.ini, and remove the ; from in
front of the php_curl extension. it's all up and running in minutes. =]
|
|
asi at neo dot ee
13-Dec-2002 02:28 |
|
If anybody has problems with getting new curl working with older version of
php then ...
I got stuck with installing curl-7.10.2+php 4.0.6 or
better said configuring PHP with mentioned version of curl.
After
some hopeless attempts to configure curl support into PHP I saw an error
message:
"checking for cURL greater than or equal to 7.8...
./configure: line 11725: test: 070a02: integer expression
expected"
after which configure said: "configure: error:
cURL version 7.8 or later is required to compile php with cURL
support"
On the mentioned line "test" is used to
compare verion from curl-sonfig with preset value 70800 but ... test
expects both values to be integer. From verson 7.8 curl outputs its
version in hex...
I did'nt had the time to think about fixing this
- I just deleted lines responsible and got curl working :)
|
|
20-Dec-2002 03:03 |
|
mwartman, perhaps you need to use https: $ch = curl_init
("https://server.com/file.req");
|
|
laurinus at blubb dot at
02-Jan-2003 05:55 |
|
Halleluja - it took me 2 Days to find this...
David Withnall
wrote: There are 2 windows install versions of php available on the php
site. the first is the executable install. the second is the zipped
binary. If you install php using the executable install it does not
install php_curl.dll If you then copy this dll out of the zipped binary
and put it into the extensions directory it will fail. This is because the
dlls in the zipped binary are not in sync with the executable install.
|
|
retro at nospam dot enx dot org
12-Jan-2003 12:42 |
|
When using cURL to do posts, make sure you actually specify the
CURLOPT_POSTFIELDS option. Not doing so seems to make PHP core (and apache
as well if you're running it through there).
|
|
jobob AT yahoo.com
23-Jan-2003 05:18 |
|
There is a site which uses curl to do a lot of automated link checking in a
script is <a href=">affiliate
source</a> which seems to be pretty interesting in the script they
use. Here is a modified version.
function GetCurlPage ($pageSpec)
{ if(function_exists('curl_init'){ $agent = "Mozilla/4.0
(compatible; MSIE 6.0; Windows NT 5.0)"; $ref = "
$ch = curl_init($pageSpec); curl_setopt($ch,
CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT,
$agent); curl_setopt($ch, CURLOPT_REFERER, $ref);
$tmp =
curl_exec ($ch); curl_close ($ch); $ret = $tmp;
}else{ $ret = @implode('', @file ($pageSpec)); } return
$ret; }
This check if CURL is installed first before attempting
to call it, this is for those of you who are not sure if it is. (shared
hosting etc.)
cheers
|
|
stain at dxml dot org
13-Feb-2003 11:33 |
|
php 4.3.0 + curl 7.10.3
in my php code i do something
like:
<code> $ch = curl_init(); curl_setopt ($ch,
CURLOPT_URL,
'https://www.example.com/path/to/file.ext');
ob_start(); curl_exec
($ch); ob_end_flush();
if (curl_error($ch))
printf("Error %s: %s", curl_errno($ch),
curl_error($ch));
curl_close ($ch); </code>
and i
retrieve an error like:
Error 35: SSL:
error:xxxxxxxx:lib(xx):func(xxx):reason(xxx)
curl error 35 is
CURLE_SSL_CONNECT_ERROR
from SSLCERTS file in curl source
directory: "Starting in 7.10, libcurl performs peer SSL
certificate verification by default. [...] If the remote server
uses a self-signed certificate, or if you don't install curl's CA cert
bundle or if it uses a certificate signed by a CA that isn't included
in the bundle, then you need to do one of the following: 1. Tell
libcurl to *not* verify the peer. With libcurl you disable with with
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); With the
curl command tool, you disable this with
-k/--insecure. [...]"
(i suggest to read all the info
provided with curl distribution)
so, in php, you need do the
following:
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
i
would be sure my connection don't crash, but since i didn't find any other
documentation available, i add also the following:
curl_setopt
($ch, CURLOPT_SSL_VERIFYHOST, 0);
from php
manual: "CURLOPT_SSL_VERIFYHOST: Pass a long if CURL should verify
the Common name of the peer certificate in the SSL handshake. A value of 1
denotes that we should check for the existence of the common name, a value
of 2 denotes that we should make sure it matches the provided hostname.
"
HTH
bye, stain.
|
|
bharris at spro dot net
25-Feb-2003 11:58 |
|
For Win2000: To get the 4.3.1 curl dll to work with https you now need to
download the latest win32 curl library from and snag the
ca-bundle.crt file from the lib directory. Place this somewhere handy on
your webserver.
Then in your PHP script, add the following setopt
line to the rest of your curl_setopt commands:
curl_setopt($ch,
CURLOPT_CAFILE, 'C:\pathto\ca-bundle.crt')
This worked for me and
allowed me to discontinue using the CURLOPT_SSL_VERIFYPEER set to zero
hack.
|
|
diana dot castillo at nvtechnologies dot com
25-Mar-2003 05:30 |
|
if you need to install curl on a windows system, this has the best
explanation:
|
|
dweingart at pobox dot com
02-Apr-2003 11:08 |
|
If you want to Curl to follow redirects and you would also like Curl to
echo back any cookies that are set in the process, use
this:
curl_setopt($ch, CURLOPT_COOKIEJAR, '-');
'-' means
stdout
-dw
|
|
thomas at NOSPAM dot gutschke dot com
07-Apr-2003 12:00 |
|
Here's a little code snippet if you need the last-modified-time for a
remote file as filemtime() does not work on those. :)
$modified =
""; function read_header($ch, $header) { global
$modified; $length = strlen($header); if(strstr($header,
"Last-Modified:")) { $modified =
substr($header, 15); } return $length; }
function
last_mod($remote_file) { global $modified; $ch =
curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_file);
curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch,
CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,
1); curl_setopt($ch, CURLOPT_HEADERFUNCTION,
'read_header');
$headers = curl_exec ($ch); curl_close
($ch); return $modified; }
use something like echo
strtotime(last_mod(")); to
get the time as an Unix timestamp or echo last_mod("); to get
the gmdate()-like time.
|
|
sorin
09-Apr-2003 08:44 |
|
function curl_string ($url,$user_agent,$proxy){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_PROXY, $proxy); curl_setopt ($ch,
CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_USERAGENT,
$user_agent); curl_setopt ($ch, CURLOPT_COOKIEJAR,
"c:\cookie.txt"); curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt
($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_TIMEOUT,
120); $result = curl_exec ($ch); curl_close($ch); return
$result; }
$url_page = " $user_agent
= "Mozilla/4.0"; $proxy = " $string
= curl_string($url_page,$user_agent,$proxy); echo $string;
this
could hep someone Sorin
|
|
yc at_ laposte dot_ net
21-May-2003 11:46 |
|
Do not try to send mutliple HTTP requests with one curl session, because
this seems to cause curl to hang randomly, though the requests are
executed without problem. So it seems much more efficient to do a
curl_close and curl_init between each request. hope this helps
|
|
add a note |
| |