获取http头部信息三种方法
第一种:PHP自带函数 get_headers($url);
第二种:打印$http_response_header变量。该变量在脚本中调用get_headers 或者使用file_get_contents($url后生效)
第三种:使用fread fopen之类函数,然后用stream_get_meta_data函数获取打开文件数据流信息
全文:http://blog.csdn.net/tstbdt/article/details/45532863
如何在php中获取curl请求的请求头信息及相应头信息
$oCurl = curl_init();
// 设置请求头
$header
[] =
"Content-type: application/x-www-form-urlencoded"
;
$user_agent
=
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"
;
curl_setopt(
$oCurl
, CURLOPT_URL,
$sUrl
);
curl_setopt(
$oCurl
, CURLOPT_HTTPHEADER,
$header
);
// 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
curl_setopt(
$oCurl
, CURLOPT_HEADER, true);
// 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文
curl_setopt(
$oCurl
, CURLOPT_NOBODY, true);
// 使用上面定义的 ua
curl_setopt(
$oCurl
, CURLOPT_USERAGENT,
$user_agent
);
curl_setopt(
$oCurl
, CURLOPT_RETURNTRANSFER, 1 );
// 不用 POST 方式请求, 意思就是通过 GET 请求
curl_setopt(
$oCurl
, CURLOPT_POST, false);
$sContent
= curl_exec(
$oCurl
);
// 获得响应结果里的:头大小
$headerSize
= curl_getinfo(
$oCurl
, CURLINFO_HEADER_SIZE);
// 根据头大小去获取头信息内容
$header
=
substr
(
$sContent
, 0,
$headerSize
);
curl_close(
$oCurl
);
转自:https://zhidao.baidu.com/question/986423438619985979.html