PHP--获取响应头(Response Header)方法

方法一:

$baiduUrl = "http://www.baidu.com/link";
 
file_get_contents($baiduUrl);
$responseInfo = $http_response_header;
 
print_r($responseInfo);
// 输出:
Array
(
    [0] => HTTP/1.1 302 Found
    [1] => Date: Fri, 27 Jun 2014 02:47:35 GMT
    [2] => Server: Apache
    [3] => Location: http://www.edeng.cn/s/chuna/
    [4] => Cache-Control: max-age=86400
    [5] => Expires: Sat, 28 Jun 2014 02:47:35 GMT
    [6] => Content-Length: 212
    [7] => Connection: Close
    [8] => Content-Type: text/html; charset=iso-8859-1
    [9] => HTTP/1.1 200 OK
    [10] => Server: nginx/1.4.3
    [11] => Date: Fri, 27 Jun 2014 02:47:35 GMT
    [12] => Content-Type: text/html; charset=utf-8
    [13] => Connection: close
    [14] => Expires: Mon, 26 Jul 1997 05:00:00 GMT
    [15] => Last-Modified: Fri, 27 Jun 2014 02:47:35 GMT
    [16] => Cache-Control: no-store, no-cache, must-revalidate
    [17] => Pragma: no-cache
    [18] => Vary: User-Agent,Accept-Encoding
    [19] => X-Cache: MISS from web1.edeng.cn
    [20] => Via: 1.1 web1.edeng.cn:80 (squid)
)
 
遍历该数组即可得到相应的值。比如要想获得 Location 的值:
 
foreach ($responseInfo as $loop) {
if(strpos($loop, "Location") !== false){
$edengUrl = trim(substr($loop, 10));
print_r($edengUrl);
// 输出: http://www.edeng.cn/s/chuna/
}
}
 
方法二:
==========================================
function get_head($sUrl){
$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);
 
return $header;
}
 
如上面解析,我们可以成功获得到头信息:
HTTP/1.1 302 Found
Date: Fri, 27 Jun 2014 02:47:35 GMT
Server: Apache
Location: http://www.edeng.cn/s/chuna/
Cache-Control: max-age=86400
Expires: Sat, 28 Jun 2014 02:47:35 GMT
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
 
这时候,如果我们想获得 Location  项的内容,可以先把上面头正文件按回车换行切割成数组,然后再遍历匹配,如:
$responseHead = post_head($baiduUrl);
 
$headArr = explode("\r\n", $responseHead);
foreach ($headArr as $loop) {
if(strpos($loop, "Location") !== false){
$edengUrl = trim(substr($loop, 10));
print_r($edengUrl);
// 输出: http://www.edeng.cn/s/chuna/
}
}
全部评论

相关推荐

12-06 01:10
已编辑
哈尔滨工程大学 Java
一面问的真细,二面不知为啥变双机位。9.29快手主站平时怎么学习 AI 的,国内外知名大模型,实习公司都用的什么大模型,怎么评估效果的java池化思想,线程池构造方法的核心参数,线程池中阻塞队列注意事项,submit方法参数和执行逻辑,shutdown和shutdownnow,核心线程允许过期吗threadlocal底层,为什么key是弱引用,key回收了再get或者set这个value会怎样aqs,如何保证公平性java代理java堆划分,新生代还有别的晋升老年代的情况吗,什么时候触发gc,gc失败抛什么异常,如何排查oom,导出dump命令redis数据结构,哪个底层是跳表,和其他数据结构对比布隆过滤器会出现大key问题吗,你咋实现的布隆过滤器你怎么实现redis分布式锁,可重入,续期聚簇索引非聚簇索引select语句会加锁吗,怎么实现的不加锁undolog redolog binlog怎么能让select加锁,update这个范围加的什么锁,update一条呢手撕简单01背包,接雨水10.10快手主站意图识别用的哪个大模型,走到意图和rag的比例,faq是点击的吗自然语言怎么识别的gap一年干啥了,转正怎么样没跟组里提意向吗,研究生研究方向是传统算法吗,会大模型微调吗注册场景为什么用布隆过滤器,原理分布式锁底层的key怎么拼的,value里是什么redis持久化zset底层mysql索引结构,一个表三个字段有主键唯一索引和没索引的字段会有几个b+树,聚簇索引非聚簇索引存的啥无手撕
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务