原創(chuàng)作品,轉(zhuǎn)載注明出處 http://sunking.sinaapp.com/?p=111
最近使用curl的時(shí)候,發(fā)現(xiàn)了一個(gè)比較好用的函數(shù),當(dāng)然是初級(jí)者適用的一個(gè)函數(shù),就是curl_getinfo(),
在抓取一個(gè)頁(yè)面的時(shí)候,會(huì)遇到302頁(yè)面跳轉(zhuǎn)的情況,剛開(kāi)始處理的時(shí)候,是用curl抓取一個(gè)域名頁(yè)面的內(nèi)容,適用curl_exec,抓取頁(yè)面全部?jī)?nèi)容,然后用正則匹配出來(lái)用戶(hù)域名url,通過(guò)此域名再次抓取此地址的內(nèi)容,這樣做挺麻煩的,后來(lái)發(fā)現(xiàn)curl_getinfo(),返回來(lái)一個(gè)數(shù)組類(lèi)型的值,里面有一個(gè)url,有一個(gè)http_code,http_code可以是302,200,404,500等,如果是302的話(huà),就是頁(yè)面跳轉(zhuǎn),直接可以得到跳轉(zhuǎn)的頁(yè)面的url。這樣,就可以直接跳過(guò)抓取域名地址哪一步,直接獲得跳轉(zhuǎn)頁(yè)面的鏈接,直接抓取內(nèi)容就好了,下面是例子:
<?php
$url = ‘sunking18.tk’;
$ch = curl_init();
$header = array ();
$header [] = ‘sunking18.tk’;
$header [] = ‘User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8′;
$header [] = ‘Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8′;
$header [] = ‘Accept-Encoding: gzip, deflate’;
$header [] = ‘Accept-Language: zh-cn,zh;q=0.5′;
$header [] = ‘Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7′;
$header [] = ‘Keep-Alive: 115′;
$header [] = ‘Connection: Keep-Alive’;
$header [] = ‘Referer: sunking18.tk’;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_TIMEOUT, 100);
curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt ($ch, CURLOPT_HEADER,true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_ENCODING, “gzip” ); //設(shè)置為客戶(hù)端支持gzip壓縮
$re = curl_exec($ch);
$res = curl_getinfo($ch);
echo “<pre>”;
print_r($res);
?>
打印結(jié)果如下:
Array ( [url] => HTTP://sunking18.tk [content_type] => text/html [http_code] => 302 [header_size] => 311 [request_size] => 387 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 1.467 [namelookup_time] => 1.014 [connect_time] => 1.03 [pretransfer_time] => 1.03 [size_upload] => 0 [size_download] => 167 [speed_download] => 113 [speed_upload] => 0 [download_content_length] => 167 [upload_content_length] => 0 [starttransfer_time] => 1.467 [redirect_time] => 0 [certinfo] => Array ( ) )
其中,url就是302跳轉(zhuǎn)頁(yè)面url,http_code就是http狀態(tài)碼,如果想要單獨(dú)獲得其中的一個(gè)參數(shù),只要在curl_getinfo($ch,****)設(shè)置你想要的參數(shù)就可以了,比如,你想要獲得http_code,就可以使用:curl_getinfo($ch,CURLINFO_HTTP_CODE),則會(huì)返回一個(gè)http_code字符串。很方便使用。
curl_getinfo 共有20個(gè)參數(shù),如下:
這個(gè)參數(shù)可能是以下常量之一:
可以根據(jù)需要設(shè)置不同的參數(shù)。
順帶說(shuō)一下,http_code的含義:
[Informational 1xx]
$http_code["0"]=”Unable to access”;
$http_code["100"]=”Continue”;
$http_code["101"]=”Switching Protocols”;
[Successful 2xx]
$http_code["200"]=”O(jiān)K”;
$http_code["201"]=”Created”;
$http_code["202"]=”Accepted”;
$http_code["203"]=”Non-Authoritative Information”;
$http_code["204"]=”No Content”;
$http_code["205"]=”Reset Content”;
$http_code["206"]=”P(pán)artial Content”;
[Redirection 3xx]
$http_code["300"]=”Multiple Choices”;
$http_code["301"]=”Moved Permanently”;
$http_code["302"]=”Found”;
$http_code["303"]=”See Other”;
$http_code["304"]=”Not Modified”;
$http_code["305"]=”Use Proxy”;
$http_code["306"]=”(Unused)”;
$http_code["307"]=”Temporary Redirect”;
[Client Error 4xx]
$http_code["400"]=”Bad Request”;
$http_code["401"]=”Unauthorized”;
$http_code["402"]=”P(pán)ayment Required”;
$http_code["403"]=”Forbidden”;
$http_code["404"]=”Not Found”;
$http_code["405"]=”Method Not Allowed”;
$http_code["406"]=”Not Acceptable”;
$http_code["407"]=”P(pán)roxy Authentication Required”;
$http_code["408"]=”Request Timeout”;
$http_code["409"]=”Conflict”;
$http_code["410"]=”Gone”;
$http_code["411"]=”Length Required”;
$http_code["412"]=”P(pán)recondition Failed”;
$http_code["413"]=”Request Entity Too Large”;
$http_code["414"]=”Request-URI Too Long”;
$http_code["415"]=”Unsupported Media Type”;
$http_code["416"]=”Requested Range Not Satisfiable”;
$http_code["417"]=”Expectation Failed”;
[Server Error 5xx]
$http_code["500"]=”Internal Server Error”;
$http_code["501"]=”Not Implemented”;
$http_code["502"]=”Bad Gateway”;
$http_code["503"]=”Service Unavailable”;
$http_code["504"]=”Gateway Timeout”;
$http_code["505"]=”HTTP Version Not Supported”;
聯(lián)系客服