php获取网页header信息的4种方法

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压缩。

时间: 2024-08-19 09:21:58

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

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

方法一:psutil模块 #!usr/bin/env python # -*- coding: utf-8 -*- import socket import psutil class NodeResource(object): def get_host_info(self): host_name = socket.gethostname() return {'host_name':host_name} def get_cpu_state(self): cpu_count = psutil.cpu

asp.net C# 获取网页源码的几种方式

1 方法 System.Net.WebClient aWebClient = new System.Net.WebClient(); aWebClient.Encoding = System.Text.Encoding.Default; Byte[] pageData = aWebClient.DownloadData(url); string nhtml = Encoding.GetEncoding("utf-8").GetString(pageData); 2方法 System.N

Android中WebView获取网页中标题 ,内容, 图片的方法

如题,在Android中WebView获取网页中标题 ,内容, 图片的方法 首先是获取标题,在new WebChromeClient(){}中重写onReceivedTitle()方法 @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); // loge.e("__页面标题__"+title); } 获取内容,是参考的这边的 http

.net中保存用户信息的九种方法

.net中保存用户信息的九种方法 在ASP.NET中,有几种保持用户请求间数据的途径--实际上太多了,使没有经验的开发者对在哪个特定的环境下使用哪个对象很困惑.为了回答这个问题,需要考虑下面三个条件: .谁需要数据? .数据需要保持多长时间? .数据集有多大? 通过回答这些问题,你能决定哪个对象为保持ASP.NET应用程序请求间数据提供了最佳的解决方案.图1列出了不同的状态管理对象并描述了什么时候使用它们.ASP.NET中添加了四个新的对象:Cache.Context.ViewState和Web

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

虚拟机是每个程序员必备的工具.本文根据最新版VirtualBox用户手册的提示,通过自己的亲自实践,给出了Win7系统与运行在其中的VirtualBox 5.0.2中的Ubuntu 14.04共享信息的三种方法,而不仅仅是建立二者的共享文件夹. 在学习新的技术时,或者做不同的测试时,为了节省硬件的目的,我们常常安装虚拟机,并在其中安装不同的操作系统.我们把运行在硬件的本来的那个操作系统称为主操作系统 (host OS),而把运行在虚拟机上面的操作系统称为客操作系统 (guest OS).这时,在

PHP中获取文件扩展名的N种方法

PHP中获取文件扩展名的N种方法 从网上收罗的,基本上就以下这几种方式: 第1种方法: function get_extension($file) { substr(strrchr($file, '.'), 1); } 第2种方法: function get_extension($file) { return substr($file, strrpos($file, '.')+1); } 第3种方法: function get_extension($file) { return end(expl

查看登陆系统用户的信息的三种方法详解

查看登陆系统用户的信息的三种方法详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.who这个命令显示可以谁在登陆,但是这个有很多的花式玩法,这个命令超简单 语法:who [OPTION]... [ FILE | ARG1 ARG2 ] 1.参数:-u,显示闲置时间,若该用户在前一分钟之内有进行任何动作,将标示成"."号,如果该用户已超过24小时没有任何动作,则标示出"old"字符串. 例如: 2.参数:-m,此参数的效果和指定"a

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

在遍历数组.集合的时候,通常要获取元素的索引,本篇体验使用Knockout获取索引的2种方法. 假设有这样的一个模型: namespace UseIndex.Models { public class Student { public int Id { get; set; } public string Name { get; set; } } } 在HomeController中,先模拟一个Student的集合,在投影出Name属性的集合,最后以Json返回给前台视图. using Syste

QT中获取选中的radioButton的两种方法(动态取得控件的objectName之后,对名字进行比较)

QT中获取选中的radioButton的两种方法 QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioButton* pbtn = qobject_cast<QRadioButton*>(ui->BG->checkedButton()); 2 QString name = pbtn->objectName(); 3 if(!QString::compare(name, "rad