Brief introduction
PHP supports Libcurl, a library created by Daniel Stenberg, which allows you to connect and communicate to many different T Ypes 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 is done with PHP ' s FTP extensi On), HTTP form based upload, proxies, cookies, and User+password authentication.
These are functions have been added in PHP 4.0.2. Demand
The CURL functions you need to install the CURL package. PHP requires that you use CURL 7.0.2-beta or higher. In PHP 4.2.3, you'll need CURL version 7.9.0 or higher. From PHP 4.3.0, you'll need a CURL version that ' s 7.9.8 or higher. PHP 5.0.0 requires a CURL version 7.10.5 or greater. installation
To use PHP ' s CURL support your must also compile PHP--with-curl[=dir] where DIR is the location of the directory Containin G The LIB and include directories. In the ' include ' directory there should be a folder named "Curl" which should the contain and easy.h files. There should is a file named Libcurl.a located in the "Lib" directory. Beginning with PHP 4.3.0 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, your must copy Libeay32.dll and Ssleay32.dll from the DLL folder O F the Php/win32 binary package to the SYSTEM folder of your Windows machine. (Ex:c:/winnt/system32 or C:/windows/system)
Reference website
牋 http://cn2.php.net/manual/zh/ref.curl.php
1. Using Post
<?php // A very simple PHP example so sends a HTTP POST to a remote site // $url = ' http://localhost/test.php '; $ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, 牋 牋 牋 牋 牋 牋 燶/n "Postvar1=value1&postvar2=value2&postvar3=value3"); curl_setopt ($ch, Curlopt_returntransfer, 1);
$result = curl_exec ($ch); Curl_close ($ch);
?> |
2. Use cookies
<?php /* This script is a example of using curl in PHP to log into on one page and Then get another page passing all cookies from the ' The ' the ' the ' the ' the ' the ' the ' I If This script is a bit more advanced it might trick the server into Thinking its Netscape and even pass a fake referer, yo look like it surfed From a local page. */
$ch = Curl_init (); curl_setopt ($ch, Curlopt_cookiejar, "/tmp/cookiefilename"); curl_setopt ($ch, Curlopt_url, "http://localhost/test.php"); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, "Name=name&password=password"); curl_setopt ($ch, curlopt_returntransfer,1); Ob_start (); Prevent any output $data = curl_exec ($ch); Execute the Curl command Ob_end_clean (); Stop preventing output
Curl_close ($ch); Unset ($ch);
$ch = Curl_init (); curl_setopt ($ch, curlopt_returntransfer,1); curl_setopt ($ch, Curlopt_cookiefile, "/tmp/cookiefilename"); curl_setopt ($ch, Curlopt_url, "http://localhost/cookie.php");
$buf 2 = curl_exec ($ch);
Curl_close ($ch);
echo "<PRE>". Htmlentities ($buf 2); ?>
|
3. File Upload
<?php
//
HTTP put to a remote site
Author:julian Bond
//
$url = "Http://some.server.com/put_script";
$localfile = "Localfile.csv";
$fp = fopen ($localfile, "R");
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_verbose, 1);
curl_setopt ($ch, curlopt_userpwd, ' User:password ');
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_put, 1);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_infile, $fp);
curl_setopt ($ch, Curlopt_infilesize, FileSize ($localfile));
$http _result = curl_exec ($ch);
$error = Curl_error ($ch);
$http _code = Curl_getinfo ($ch, Curlinfo_http_code);
Curl_close ($ch);
Fclose ($FP);
Print $http _code;
print "<br/><br/> $http _result";
if ($error) {
print "<br/><br/> $error";
}
?>