最近使用curl的时候,发现了一个比较好用的函数,固然是初级者适用的一个函数,就是curl_getinfo(),php
在抓取一个页面的时候,会遇到302页面跳转的状况,刚开始处理的时候,是用curl抓取一个域名页面的内容,适用curl_exec,抓取页面所有内容,而后用正则匹配出来用户域名url,经过此域名再次抓取此地址的内容,这样作挺麻烦的,后来发现curl_getinfo(),返回来一个数组类型的值,里面有一个url,有一个http_code,http_code能够是302,200,404,500等,若是是302的话,就是页面跳转,直接能够获得跳转的页面的url。这样,就能够直接跳过抓取域名地址哪一步,直接得到跳转页面的连接,直接抓取内容就行了,下面是例子:html
<?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’;web
$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);数组
?>服务器
打印结果以下:app
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 ( ) )curl
其中,url就是302跳转页面url,http_code就是http状态码,若是想要单独得到其中的一个参数,只要在curl_getinfo($ch,****)设置你想要的参数就能够了,好比,你想要得到http_code,就可使用:curl_getinfo($ch,CURLINFO_HTTP_CODE),则会返回一个http_code字符串。很方便使用。函数
curl_getinfo 共有20个参数,以下:ui
这个参数多是如下常量之一:url
CURLINFO_EFFECTIVE_URL – 最后一个有效的URL地址
CURLINFO_HTTP_CODE – 最后一个收到的HTTP代码
CURLINFO_FILETIME – 远程获取文档的时间,若是没法获取,则返回值为“-1”
CURLINFO_TOTAL_TIME – 最后一次传输所消耗的时间
CURLINFO_NAMELOOKUP_TIME – 名称解析所消耗的时间
CURLINFO_CONNECT_TIME – 创建链接所消耗的时间
CURLINFO_PRETRANSFER_TIME – 从创建链接到准备传输所使用的时间
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”;