获取服务器Header信息代码

获取服务器Header信息代码


<?php

$url = 'http://www.mogublog.net';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$temp=curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
$num = $info[http_code];
}
curl_close($ch);

echo "网址:".$info[url]."<br>";
echo "内容编码:".$info[content_type]."<br>";
echo "HTTP状态码:".$info[http_code]."<br>";
echo "Header大小:".$info[header_size]."<br>";
echo "请求的大小:".$info[request_size]."<br>";
echo "文件创建时间:".$info[filetime]."<br>";
echo "SSL验证结果:".$info[ssl_verify_result]."<br>";
echo "跳转技术:".$info[redirect_count]."<br>";
echo "总耗时:".$info[total_time]."<br>";
echo "DNS查询耗时:".$info[namelookup_time]."<br>";
echo "等待连接耗时:".$info[connect_time]."<br>";
echo "传输前准备耗时:".$info[pretransfer_time]."<br>";
echo "上传数据的大小:".$info[size_upload]."<br>";
echo "下载数据的大小:".$info[size_download]."<br>";
echo "下载速度:".$info[speed_download]."<br>";
echo "上传速度:".$info[speed_upload]."<br>";
echo "下载内容的长度:".$info[download_content_length]."<br>";
echo "上传内容的长度:".$info[upload_content_length]."<br>";
echo "开始传输的时间:".$info[starttransfer_time]."<br>";
echo "重定向耗时:".$info[redirect_time]."<br>";

?>

另一种方法:


$header = array(
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
    'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
    'Accept-Encoding: gzip, deflate',
    'Connection: keep-alive',
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com');
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_exec($curl);
curl_close($curl);

发表评论