php获取网页header信息的方法多种多样,就php语言来说,我知道的方法有4种, 下面逐一献上。

方法一:使用get_headers()函数

推荐指数: ★★★★★

get_header方法最简单只要两行代码即可搞定。如下:

  1. $thisurl = "http://www.lao8.org/";
  2. print_r(get_headers($thisurl, 1));

得到的结果为:

  1. Array
  2. (
  3. [0] => HTTP/1.1 200 OK
  4. [Cache-Control] => max-age=86400
  5. [Content-Length] => 76102
  6. [Content-Type] => text/html
  7. [Content-Location] => http://www.lao8.org/index.html
  8. [Last-Modified] => Fri, 19 Jul 2013 03:52:30 GMT
  9. [Accept-Ranges] => bytes
  10. [ETag] => "50bc48643384ce1:5cb3"
  11. [Server] => Microsoft-IIS/6.0
  12. [X-Powered-By] => ASP.NET
  13. [Date] => Fri, 19 Jul 2013 09:06:39 GMT
  14. [Connection] => close
  15. )

方法二:使用http_response_header

推荐指数: ★★★

http_response_headerf方法也很简单,仅三行:

  1. $thisurl = "http://www.lao8.org";
  2. $html = file_get_contents($thisurl );
  3. print_r($http_response_header);

得到的结果为:

  1. Array
  2. (
  3. [0] => HTTP/1.1 200 OK
  4. [1] => Cache-Control: max-age=86400
  5. [2] => Content-Length: 76102
  6. [3] => Content-Type: text/html
  7. [4] => Content-Location: http://www.lao8.org/index.html
  8. [5] => Last-Modified: Fri, 19 Jul 2013 03:52:30 GMT
  9. [6] => Accept-Ranges: bytes
  10. [7] => ETag: "50bc48643384ce1:5cb3"
  11. [8] => Server: Microsoft-IIS/6.0
  12. [9] => X-Powered-By: ASP.NET
  13. [10] => Date: Fri, 19 Jul 2013 09:06:41 GMT
  14. [11] => Connection: close
  15. )

方法三:使用stream_get_meta_data()函数

推荐指数: ★★★

使用stream_get_meta_data()代码也只需三行:

  1. $thisurl = "http://www.lao8.org/";
  2. $fp = fopen($thisurl, 'r');
  3. print_r(stream_get_meta_data($fp));

得到的结果为:

  1. Array
  2. (
  3. [wrapper_data] => Array
  4. (
  5. [0] => HTTP/1.1 200 OK
  6. [1] => Cache-Control: max-age=86400
  7. [2] => Content-Length: 76102
  8. [3] => Content-Type: text/html
  9. [4] => Content-Location: http://www.lao8.org/index.html
  10. [5] => Last-Modified: Fri, 19 Jul 2013 03:52:30 GMT
  11. [6] => Accept-Ranges: bytes
  12. [7] => ETag: "50bc48643384ce1:5cb3"
  13. [8] => Server: Microsoft-IIS/6.0
  14. [9] => X-Powered-By: ASP.NET
  15. [10] => Date: Fri, 19 Jul 2013 09:06:41 GMT
  16. [11] => Connection: close
  17. )
  18. [wrapper_type] => http
  19. [stream_type] => tcp_socket
  20. [mode] => r+
  21. [unread_bytes] => 1086
  22. [seekable] =>
  23. [uri] => http://www.lao8.org/
  24. [timed_out] =>
  25. [blocked] => 1
  26. [eof] =>
  27. )

第四种方法: 使用php的高级函数 CURL()来获取

推荐指数: ★★★★

上面的三种方法能获取一般的网页header信息,如果想要获取更详细的header信息比如网页是否启用了GZip压缩。这时候可以用php的高级函数curl()来获取。

使用curl获得header可以检测GZip压缩
先贴出代码:

  1. <?php
  2. $szUrl = 'http://www.lao8.org/';
  3. $curl = curl_init();
  4. curl_setopt($curl, CURLOPT_URL, $szUrl);
  5. curl_setopt($curl, CURLOPT_HEADER, 1);  //输出header信息
  6. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  //不显示网页内容
  7. curl_setopt($curl, CURLOPT_ENCODING, ''); //允许执行gzip
  8. $data=curl_exec($curl);
  9. if(!curl_errno($curl))
  10. {
  11. $info = curl_getinfo($curl);
  12. $httpHeaderSize = $info['header_size'];  //header字符串体积
  13. $pHeader = substr($data, 0, $httpHeaderSize); //获得header字符串
  14. $split   = array("rn", "n", "r");  //需要格式化header字符串
  15. $pHeader = str_replace($split, '<br>', $pHeader); //使用<br>换行符格式化输出到网页上
  16. echo $pHeader;
  17. }
  18. ?>

输出结果如下:

  1. HTTP/1.1 200 OK
  2. Cache-Control: max-age=86400
  3. Content-Length: 15189
  4. Content-Type: text/html
  5. Content-Encoding: gzip
  6. Content-Location: http://www.lao8.org/index.html
  7. Last-Modified: Fri, 19 Jul 2013 03:52:28 GMT
  8. Accept-Ranges: bytes
  9. ETag: "0268633384ce1:5cb3"
  10. Vary: Accept-Encoding
  11. Server: Microsoft-IIS/6.0
  12. X-Powered-By: ASP.NET
  13. Date: Fri, 19 Jul 2013 09:27:21 GMT

可以看到使用curl获取到的header信息多了这行:Content-Encoding: gzip,网页启用了GZip压缩。

php获取网页header信息的4种方法的更多相关文章

  1. 用Python获取Linux资源信息的三种方法

    方法一:psutil模块 #!usr/bin/env python # -*- coding: utf-8 -*- import socket import psutil class NodeReso ...

  2. JAVA中获取文件MD5值的四种方法

    JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...

  3. 这里给大家介绍一下通过 Wi-Fi 连接“慷慨捐赠”你的身份信息的七种方法.

    这里给大家介绍一下通过 Wi-Fi 连接“慷慨捐赠”你的身份信息的七种方法和反制措施. 本文作者:黑子小盆友 1.利用免费热点 它们似乎无处不在,而且它们的数量会在接下来四年里增加三倍.但是它们当中很 ...

  4. Win7系统与它的Virtualbox中安装的Ubuntu14.04共享信息的几种方法

    虚拟机是每一个程序猿必备的工具.本文依据最新版VirtualBox用户手冊的提示,通过自己的亲自实践,给出了Win7系统与执行在当中的VirtualBox 5.0.2中的Ubuntu 14.04共享信 ...

  5. MYSQL获取自增ID的四种方法

    MYSQL获取自增ID的四种方法 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID 是与tabl ...

  6. Android获取APK包名的几种方法

    Android获取APK包名的几种方法:1.adb shell pm list package -f | findstr 关键字 #只能获取到包名,主Activity名无法获取到 2.使用aapt-- ...

  7. PHP获取文件后缀名的三种方法

    如下: <? PHP获取文件后缀名的几种方法1: function get_file_type($filename){ $type = substr($filename, strrpos($fi ...

  8. Knockout获取数组元素索引的2种方法,在MVC中实现

    原文:Knockout获取数组元素索引的2种方法,在MVC中实现 在遍历数组.集合的时候,通常要获取元素的索引,本篇体验使用Knockout获取索引的2种方法. 假设有这样的一个模型: namespa ...

  9. [Q]“获取AutoCAD安装信息时失败...”解决方法

    “获取AutoCAD安装信息时失败...”解决方法:在“setup.exe”上右键,以管理员权限运行即可.

随机推荐

  1. Why string is immutable in Java ?

    This is an old yet still popular question. There are multiple reasons that String is designed to be ...

  2. poj 2887 Big String

    题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do ...

  3. OpenStack:安装Glance

    >安装Glance1. 安装# apt-get install glance python-glanceclient删除sqlite文件rm -f /var/lib/glance/glance. ...

  4. 六大Nagios常见问题解决办法

    Nagios常见问题1: It appears as though you do not have permission to view information for any of the host ...

  5. 自定义Drawable

    本文由 伯乐在线 - treesouth 翻译,toolate 校稿.未经许可,禁止转载! 英文出处:ryanharter.com.欢迎加入翻译小组. 我们看过一些博客文章,讲述了为什么要适时地使用自 ...

  6. 60.ISE PhysDesignRules ERROR

    PhysDesignRules:2100 - Issue with pin connections and/or configuration on block:<U_ila_pro_0/U0/I ...

  7. Android实现Button事件的处理

    Android实现Button事件的处理 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是最基本的线性布局,给每个控件设立id值,以供代 ...

  8. 学习jax-ws(一)

    1.生成文件时提示class not find ,需要加个cp .,这样就行了 E:\mylearn\learn_webservice\learnJax-ws\bin>wsgen -cp . w ...

  9. cocos2dx中导演的职责有哪些?

    1.一个游戏里面只有一个导演,因此采用了单例的设计模式,用getInstance方法来获得 2.游戏中导演负责openGL ES的初始化,场景的切换,游戏的暂停继续(相当于拍电影的ka),节点坐标与世 ...

  10. Scrum仪式之Sprint计划会议

    会议时间:4.15.晚八点 会议地点:基础教学楼二楼 会议进程 • 首先我们讨论了实验第一个Sprint1要实现的功能,我们的初期目标.•  然后我们进一步梳理了第一阶段的任务和需求.•  之后对任务 ...