Skip to content

Instantly share code, notes, and snippets.

@cloudsben
Last active December 15, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudsben/5210435 to your computer and use it in GitHub Desktop.
Save cloudsben/5210435 to your computer and use it in GitHub Desktop.
获取http响应的头信息
<?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