Curl学习笔记


简介

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 CURL package. PHP requires that you use CURL 7.0.2-beta or higher. In PHP 4.2.3, you will need CURL version 7.9.0 or higher. From PHP 4.3.0, you will need a CURL version that's 7.9.8 or higher. PHP 5.0.0 requires a CURL version 7.10.5 or greater.

安装

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 SYSTEM folder of your Windows machine. (Ex: C:/WINNT/SYSTEM32 or C:/WINDOWS/SYSTEM)

参考网站
牋  http://cn2.php.net/manual/zh/ref.curl.php
1.使用POST
<?php
//
// A very simple PHP example that 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.使用COOKIE
<?php
/*
This script is an example of using curl in php to log into on one page and
then get another page passing all cookies from the first page along with you.
If this script was 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");

$buf2 = curl_exec ($ch);

curl_close ($ch);

echo "<PRE>".htmlentities($buf2);
?>
3.文件上传
 
<?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";

}



?>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值