转:http://blog.sina.com.cn/s/blog_5f54f0be0102uvxu.html

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

方法一:

==========================================
$baiduUrl = "http://www.baidu.com/link?url=LZE_J6a1AcieLlTzNxUZQVpe2trQ99zx1ls85ux8dXaGlFB3eiEm_Y6SJC1sNQf_";
 
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/
}
}

转:PHP--获取响应头(Response Header)方法的更多相关文章

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

    方法一: $baiduUrl = "http://www.baidu.com/link";   file_get_contents($baiduUrl); $responseInf ...

  2. 怎样获取响应头: Response Header

    1. 使用 xhr.getResponseHeader()可以获取指定响应头字段值. function getHeaderTime() { console.log(this.getResponseHe ...

  3. 【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)

    问题描述 在Azure App Service上部署了站点,想要在网站的响应头中加一个字段(Cache-Control),并设置为固定值(Cache-Control:no-store) 效果类似于本地 ...

  4. PHP获取http头信息和CI中获取HTTP头信息的方法

    CI中获取HTTP头信息的方法: $this->input->request_headers() 在不支持apache_request_headers()的非Apache环境非常有用.返回 ...

  5. 转:http协议学习系列(响应头---Response Headers)

    HTTP最常见的响应头如下所示: ·Allow:服务器支持哪些请求方法(如GET.POST等): ·Content-Encoding:文档的编码(Encode)方法.只有在解码之后才可以得到Conte ...

  6. 更改TestStep的request header和获取TestStep的response header

    更改TestStep的request header for example def userId = "xxxxxxxxxxxxx" def request = context.t ...

  7. 请求头(request headers)和响应头(response headers)解析

    *****************请求头(request headers)***************** POST /user/signin HTTP/1.1    --请求方式 文件名 http ...

  8. axios获取响应头

    [转载] 来源:https://segmentfault.com/a/1190000009125333 在用 axios 获取 respose headers 时候获取到的只有的 Object { c ...

  9. [面试没答上的问题1]http请求,请求头和响应头都有什么信息?

    最近在找工作,面试官问了一些问题自己并没有回答上,这里做一个小结. http请求,请求头和响应头都有什么信息? 页面和服务器交互最常见的方式就是ajax,ajax简单来说是浏览器发送请求到服务端,然后 ...

随机推荐

  1. oracle重建控制文件

    根据已有数据库创建新的控制文件#数据库必须是mounted或open状态 sql> alter database backup controlfile to trace; 可以使用以下快捷方式找 ...

  2. 如何在断开ssh连接后仍然保持服务器正常运行程序

    问题描述:当SSH远程连接到服务器上,然后运行一个Python程序(bpr.py),然后把终端开闭(切断SSH连接)之后,发现该程序执行中断. 解决方法:使用nohup命令让程序在关闭窗口(切换SSH ...

  3. Leetcode: 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  4. SpringMvc异常处理

    SpringMvc通过HandlerExceptionResolver处理程序的异常,包括Handler映射.数据绑定.以及方法执行时发生的异常,SpringMvc提供的HandlerExceptio ...

  5. 三个 DAL 相关的Java代码小工具

    最近在做 DAL (Data Access Layer 数据访问层) 的服务化,发现有不少地方是人工编写比较繁琐的,因此写了几个小工具来完成. 1.  从 DAO 类自动生成 CoreService ...

  6. python 笔记

    第一周2016/9/11   Python 2.0和3.0的区别 3.0 的模块名改了和之前的2.0 不一样 #!/usr/bin/env python # -*- coding:utf-8 -*- ...

  7. 关于在官网上查看和下载特定版本的webrtc代码

    注:这个方法已经不适用了,帖子没删只是留个纪念而已 gclient:如果不知道gclient是什么东西 ... 就别再往下看了. 下载特定版本的代码: #gclient sync --revision ...

  8. Sprint第二个冲刺(第八天)

    一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图:

  9. 怎么简单获取input file 选中的图片,并在一个div的img里面赋值src实现预览?

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...

  10. Pycharm使用问题# 快捷键设置

    Pycharm内部设置也已经变得非常复杂,此处说明我改变字体大小的快捷键设置. 在Settings中找到Keymap一项,默认显示会展开Editor Actions一项: 在里面分别找到Decreas ...