curl_getinfo的巧用


最近使用curl的時候,發現了一個比較好用的函數,當然是初級者適用的一個函數,就是curl_getinfo(),

在抓取一個頁面的時候,會遇到302頁面跳轉的情況,剛開始處理的時候,是用curl抓取一個域名頁面的內容,適用curl_exec,抓取頁面全部內容,然后用正則匹配出來用戶域名url,通過此域名再次抓取此地址的內容,這樣做挺麻煩的,后來發現curl_getinfo(),返回來一個數組類型的值,里面有一個url,有一個http_code,http_code可以是302,200,404,500等,如果是302的話,就是頁面跳轉,直接可以得到跳轉的頁面的url。這樣,就可以直接跳過抓取域名地址哪一步,直接獲得跳轉頁面的鏈接,直接抓取內容就好了,下面是例子:

<?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” ); //設置為客戶端支持gzip壓縮 $re  = curl_exec($ch); $res = curl_getinfo($ch); echo “<pre>”; print_r($res);

?>

打印結果如下:

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跳轉頁面url,http_code就是http狀態碼,如果想要單獨獲得其中的一個參數,只要在curl_getinfo($ch,**)設置你想要的參數就可以了,比如,你想要獲得http_code,就可以使用:curl_getinfo($ch,CURLINFO_HTTP_CODE),則會返回一個http_code字符串。很方便使用。

curl_getinfo 共有20個參數,如下:

這個參數可能是以下常量之一:

  • CURLINFO_EFFECTIVE_URL – 最后一個有效的URL地址
  • CURLINFO_HTTP_CODE – 最后一個收到的HTTP代碼
  • CURLINFO_FILETIME – 遠程獲取文檔的時間,如果無法獲取,則返回值為“-1”
  • CURLINFO_TOTAL_TIME – 最后一次傳輸所消耗的時間
  • CURLINFO_NAMELOOKUP_TIME – 名稱解析所消耗的時間
  • CURLINFO_CONNECT_TIME – 建立連接所消耗的時間
  • CURLINFO_PRETRANSFER_TIME大專欄  curl_getinfo的巧用ong> – 從建立連接到准備傳輸所使用的時間
  • CURLINFO_STARTTRANSFER_TIME – 從建立連接到傳輸開始所使用的時間
  • CURLINFO_REDIRECT_TIME – 在事務傳輸開始前重定向所使用的時間
  • CURLINFO_SIZE_UPLOAD – 上傳數據量的總值
  • CURLINFO_SIZE_DOWNLOAD – 下載數據量的總值
  • CURLINFO_SPEED_DOWNLOAD – 平均下載速度
  • CURLINFO_SPEED_UPLOAD – 平均上傳速度
  • CURLINFO_HEADER_SIZE – header部分的大小
  • CURLINFO_HEADER_OUT – 發送請求的字符串
  • CURLINFO_REQUEST_SIZE – 在HTTP請求中有問題的請求的大小
  • CURLINFO_SSL_VERIFYRESULT – 通過設置CURLOPT_SSL_VERIFYPEER返回的SSL證書驗證請求的結果
  • CURLINFO_CONTENT_LENGTH_DOWNLOAD – 從Content-Length: field中讀取的下載內容長度
  • CURLINFO_CONTENT_LENGTH_UPLOAD – 上傳內容大小的說明
  • CURLINFO_CONTENT_TYPE – 下載內容的Content-Type:值,NULL表示服務器沒有發送有效的Content-Type:header

可以根據需要設置不同的參數。

順帶說一下,http_code的含義:

[Informational 1xx]

$http_code[“0”]=”Unable to access”; $http_code[“100”]=”Continue”; $http_code[“101”]=”Switching Protocols”;

[Successful 2xx] $http_code[“200”]=”OK”; $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”]=”Partial 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”]=”Payment Required”; $http_code[“403”]=”Forbidden”; $http_code[“404”]=”Not Found”; $http_code[“405”]=”Method Not Allowed”; $http_code[“406”]=”Not Acceptable”; $http_code[“407”]=”Proxy Authentication Required”; $http_code[“408”]=”Request Timeout”; $http_code[“409”]=”Conflict”; $http_code[“410”]=”Gone”; $http_code[“411”]=”Length Required”; $http_code[“412”]=”Precondition 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”;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM