最近使用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个参数,如下:
这个参数可能是以下常量之一:
可以根据需要设置不同的参数。
顺带说一下,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”;
原文:https://www.cnblogs.com/lijianming180/p/12041419.html