Last active
December 15, 2015 05:39
-
-
Save cloudsben/5210435 to your computer and use it in GitHub Desktop.
获取http响应的头信息
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
获取http响应的头信息 | |
parameter: | |
$url : the target url | |
$formate: if non-zero,function parses the response and sets the array's keys | |
return : | |
array | |
return example: | |
Array | |
( | |
[url] => http://www.csdn.net/article/2013-03-11/2814436-Google-10-rules-for-designing-data-centers | |
[content_type] => text/html; charset=utf-8 | |
[http_code] => 200 | |
[header_size] => 165 | |
[request_size] => 228 | |
[filetime] => -1 | |
[ssl_verify_result] => 0 | |
[redirect_count] => 0 | |
[total_time] => 0.218763 | |
[namelookup_time] => 0.098122 | |
[connect_time] => 0.106448 | |
[pretransfer_time] => 0.106491 | |
[size_upload] => 0 | |
[size_download] => 0 | |
[speed_download] => 0 | |
[speed_upload] => 0 | |
[download_content_length] => -1 | |
[upload_content_length] => 0 | |
[starttransfer_time] => 0.218702 | |
[redirect_time] => 0 | |
) | |
*/ | |
if ( ! function_exists('get_headers_by_curl')) | |
{ | |
function get_headers_by_curl($url) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); | |
curl_setopt($ch, CURLOPT_HEADER, true); //包含 HTTP 头 | |
curl_setopt($ch, CURLOPT_NOBODY, true); //不读取页面内容 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 15); | |
curl_setopt($ch, CURLOPT_USERAGENT, USER_AGENT); | |
curl_setopt($ch, CURLOPT_VERBOSE, false); | |
$r = curl_exec($ch); | |
if(!$r) | |
{ | |
$headers = FALSE; | |
}else | |
{ | |
$headers = curl_getinfo($ch); | |
} | |
curl_close($ch); | |
return $headers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment