首页 » 网络 » curl与PHP

curl API

setopt() - getinfo() -

curl_setopt()

bool curl_setopt(resource $ch, int $option, mixed $value);

常用的选项

选项说明
CURLOPT_RETURNTRANSFERcurl_exec()执行结果不输出到STDOUT,而是作为字符串返回。
CURLOPT_VERBOSE输出curl与服务器交互的详细信息(调试用)。
CURLOPT_FOLLOWLOCATION自动跳转到"Location:"指定的链接。
CURLOPT_POST执行HTTP POST。类型为application/x-www-form-urlencoded。
CURLOPT_POSTFIELDSHTTP POST的内容。
CURLOPT_COOKIEJAR保存cookie到文件。
CURLOPT_COOKIEFILE从文件读取cookie。

CURLOPT_VERBOSE - 输出相信信息到STDERR。通常会写到Apache error_access文件。输出的信息如:

> GET /item.htm?id=35560035021 HTTP/1.1
Host: item.taobao.com
Accept: */*

< HTTP/1.1 200 OK
< Server: Tengine
< Date: Sat, 02 Nov 2013 06:12:45 GMT
< Content-Type: text/html;charset=GBK
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Cache-Control: max-age=3
< At_Autype: 5_36657848
< At_Cat: item_50013878
< X-Category: /cat/50013864
< At_Nick: simpler%E8%A5%BF
< At_Itemid: 35560035021
< At_Isb: 0
< Content-Language: zh-CN
< X-Cache: HIT TCP_MEM_HIT dirn:-2:-2
< Via: wagbridge010238184014.cm4:8888
< Age: 6732
< 
* Connection #0 to host item.taobao.com left intact
* * Closing connection #0

CURLOPT_FOLLOWLOCATION - 跳转到"Location:"指定的链接,即自动追随重定向。例如访问豆瓣:

$ch = curl_init("http://douban.com");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_exec($ch);

输出(注意第一次输出的Host是douban.com,结果被重定向到www.douban.com)

> GET / HTTP/1.1
Host: douban.com
Accept: */*

< HTTP/1.1 301 Moved Permanently
< Location: http://www.douban.com/
...

> GET / HTTP/1.1
Host: www.douban.com
Accept: */*

< HTTP/1.1 200 OK

CURLOPT_POSTFIELDS - HTTP POST的内容。支持两种形式,一种是"para1=val1&para2=val2&...",另一种是传递数组作为参数,选取不同的形式将影响Content-Type的值,如果是前者,Content-Type将是application/x-www-form-urlencoded,如果是后者,Content-Type将是application/form-data。如果提交文件,则需以@打头,其后可跟随文件类型,即附加";type=mimetype"。

$data = array(
    "name"  => "berlinix",
    "password"  => "basic@it",
    );

$pdata = "";
foreach($data as $k => $v) {
    $pdata .= "$k=" . urlencode($v) . "&";
}
$pdata = substr($pdata, 0, -1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $pdata);   # application/x-www-form-urlencoded
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    # application/form-data
$ck = tempnam("/tmp", "CURLCOOKIE");

$ch = curl_init("http://www.douban.com");
curl_setopt($ch, CURLOPT_COOKIEJAR, $ck);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);

查看cookie文件:

$ less /tmp/CURLCOOKIErwvd3L 
.douban.com     TRUE    /       FALSE   1378042664      bid     "iPa2M7Ra4ks"

curl_getinfo()

mixed curl_getinfo(resource $ch[, int $opt = 0]);

curl_getinfo()用于获取最后一次传输的信息,如果不指定opt,则返回一个关联数组。下面一段代码简单浏览这个函数的返回信息:

$url = "http://weibo.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$resp = curl_getinfo($ch);
print_r($resp);
[url] => http://weibo.com
[content_type] => text/html; charset=utf-8
[http_code] => 200
[header_size] => 701
[request_size] => 48
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 5.455881
[namelookup_time] => 5.211035
[connect_time] => 5.228934
[pretransfer_time] => 5.228943
[size_upload] => 0
[size_download] => 32864
[speed_download] => 6023
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 5.350912
[redirect_time] => 0

分享

0