php获取网页header信息的4种方法
php获取网页header信息的方法多种多样,就php语言来说,我知道的方法有4种, 下面逐一献上。
方法一:使用get_headers()函数
推荐指数: ★★★★★
get_header方法最简单只要两行代码即可搞定。如下:
- $thisurl = "http://www.lao8.org/";
- print_r(get_headers($thisurl, 1));
得到的结果为:
- Array
- (
- [0] => HTTP/1.1 200 OK
- [Cache-Control] => max-age=86400
- [Content-Length] => 76102
- [Content-Type] => text/html
- [Content-Location] => http://www.lao8.org/index.html
- [Last-Modified] => Fri, 19 Jul 2013 03:52:30 GMT
- [Accept-Ranges] => bytes
- [ETag] => "50bc48643384ce1:5cb3"
- [Server] => Microsoft-IIS/6.0
- [X-Powered-By] => ASP.NET
- [Date] => Fri, 19 Jul 2013 09:06:39 GMT
- [Connection] => close
- )
方法二:使用http_response_header
推荐指数: ★★★
http_response_headerf方法也很简单,仅三行:
- $thisurl = "http://www.lao8.org";
- $html = file_get_contents($thisurl );
- print_r($http_response_header);
得到的结果为:
- Array
- (
- [0] => HTTP/1.1 200 OK
- [1] => Cache-Control: max-age=86400
- [2] => Content-Length: 76102
- [3] => Content-Type: text/html
- [4] => Content-Location: http://www.lao8.org/index.html
- [5] => Last-Modified: Fri, 19 Jul 2013 03:52:30 GMT
- [6] => Accept-Ranges: bytes
- [7] => ETag: "50bc48643384ce1:5cb3"
- [8] => Server: Microsoft-IIS/6.0
- [9] => X-Powered-By: ASP.NET
- [10] => Date: Fri, 19 Jul 2013 09:06:41 GMT
- [11] => Connection: close
- )
方法三:使用stream_get_meta_data()函数
推荐指数: ★★★
使用stream_get_meta_data()代码也只需三行:
- $thisurl = "http://www.lao8.org/";
- $fp = fopen($thisurl, 'r');
- print_r(stream_get_meta_data($fp));
得到的结果为:
- Array
- (
- [wrapper_data] => Array
- (
- [0] => HTTP/1.1 200 OK
- [1] => Cache-Control: max-age=86400
- [2] => Content-Length: 76102
- [3] => Content-Type: text/html
- [4] => Content-Location: http://www.lao8.org/index.html
- [5] => Last-Modified: Fri, 19 Jul 2013 03:52:30 GMT
- [6] => Accept-Ranges: bytes
- [7] => ETag: "50bc48643384ce1:5cb3"
- [8] => Server: Microsoft-IIS/6.0
- [9] => X-Powered-By: ASP.NET
- [10] => Date: Fri, 19 Jul 2013 09:06:41 GMT
- [11] => Connection: close
- )
- [wrapper_type] => http
- [stream_type] => tcp_socket
- [mode] => r+
- [unread_bytes] => 1086
- [seekable] =>
- [uri] => http://www.lao8.org/
- [timed_out] =>
- [blocked] => 1
- [eof] =>
- )
第四种方法: 使用php的高级函数 CURL()来获取
推荐指数: ★★★★
上面的三种方法能获取一般的网页header信息,如果想要获取更详细的header信息比如网页是否启用了GZip压缩。这时候可以用php的高级函数curl()来获取。
使用curl获得header可以检测GZip压缩
先贴出代码:
- <?php
- $szUrl = 'http://www.lao8.org/';
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $szUrl);
- curl_setopt($curl, CURLOPT_HEADER, 1); //输出header信息
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //不显示网页内容
- curl_setopt($curl, CURLOPT_ENCODING, ''); //允许执行gzip
- $data=curl_exec($curl);
- if(!curl_errno($curl))
- {
- $info = curl_getinfo($curl);
- $httpHeaderSize = $info['header_size']; //header字符串体积
- $pHeader = substr($data, 0, $httpHeaderSize); //获得header字符串
- $split = array("rn", "n", "r"); //需要格式化header字符串
- $pHeader = str_replace($split, '<br>', $pHeader); //使用<br>换行符格式化输出到网页上
- echo $pHeader;
- }
- ?>
输出结果如下:
- HTTP/1.1 200 OK
- Cache-Control: max-age=86400
- Content-Length: 15189
- Content-Type: text/html
- Content-Encoding: gzip
- Content-Location: http://www.lao8.org/index.html
- Last-Modified: Fri, 19 Jul 2013 03:52:28 GMT
- Accept-Ranges: bytes
- ETag: "0268633384ce1:5cb3"
- Vary: Accept-Encoding
- Server: Microsoft-IIS/6.0
- X-Powered-By: ASP.NET
- Date: Fri, 19 Jul 2013 09:27:21 GMT
可以看到使用curl获取到的header信息多了这行:Content-Encoding: gzip,网页启用了GZip压缩。
php获取网页header信息的4种方法的更多相关文章
- 用Python获取Linux资源信息的三种方法
方法一:psutil模块 #!usr/bin/env python # -*- coding: utf-8 -*- import socket import psutil class NodeReso ...
- JAVA中获取文件MD5值的四种方法
JAVA中获取文件MD5值的四种方法其实都很类似,因为核心都是通过JAVA自带的MessageDigest类来实现.获取文件MD5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过Messa ...
- 这里给大家介绍一下通过 Wi-Fi 连接“慷慨捐赠”你的身份信息的七种方法.
这里给大家介绍一下通过 Wi-Fi 连接“慷慨捐赠”你的身份信息的七种方法和反制措施. 本文作者:黑子小盆友 1.利用免费热点 它们似乎无处不在,而且它们的数量会在接下来四年里增加三倍.但是它们当中很 ...
- Win7系统与它的Virtualbox中安装的Ubuntu14.04共享信息的几种方法
虚拟机是每一个程序猿必备的工具.本文依据最新版VirtualBox用户手冊的提示,通过自己的亲自实践,给出了Win7系统与执行在当中的VirtualBox 5.0.2中的Ubuntu 14.04共享信 ...
- MYSQL获取自增ID的四种方法
MYSQL获取自增ID的四种方法 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID 是与tabl ...
- Android获取APK包名的几种方法
Android获取APK包名的几种方法:1.adb shell pm list package -f | findstr 关键字 #只能获取到包名,主Activity名无法获取到 2.使用aapt-- ...
- PHP获取文件后缀名的三种方法
如下: <? PHP获取文件后缀名的几种方法1: function get_file_type($filename){ $type = substr($filename, strrpos($fi ...
- Knockout获取数组元素索引的2种方法,在MVC中实现
原文:Knockout获取数组元素索引的2种方法,在MVC中实现 在遍历数组.集合的时候,通常要获取元素的索引,本篇体验使用Knockout获取索引的2种方法. 假设有这样的一个模型: namespa ...
- [Q]“获取AutoCAD安装信息时失败...”解决方法
“获取AutoCAD安装信息时失败...”解决方法:在“setup.exe”上右键,以管理员权限运行即可.
随机推荐
- 分享我常用的一些JS验证和函数
下面是我常用一些JS验证和函数,有一些验证我直接写到了对象的属性里面了,可以直接通过对象.方法来调用//浮点数除法运算 function fdiv(a, b, n) { if (n == undefi ...
- Oracle Database Links解析
什么是Database Links呢? 首先我们阐述下它的作用:使用户可以通过一个数据库访问到另外一个远程数据库. 那么Database Link是存储着远程数据库的连接信息. 如下图所示: 用户Sc ...
- virtualbox cannot access the kernel driver的解决办法
一位网友windows xp sp3下安装virtualbox 4.1.20版本,安装好了重启过后,可以打开virtualbox,但是等到创建好虚拟电脑后按启动按钮,就出现了错误提示:"Ca ...
- IOS内存管理「2」- 点语法的内存管理
- C# 生成XML 多级节点
直接上代码: 在应用程序中 class Program { //public static JsonServiceClient Service = new JsonServiceClient(Conf ...
- 新安装Ubuntu加载时提示“为/检查磁盘时发生严重错误”的解决方法
本文部分内容转载自: http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6419.html http://www.aichengxu.com/v ...
- 【转】linux root用户ifconfig报command not found
解决办法: 方法一: 直接输入su - 回车.就可以ifconfig了 方法二: /etc/profile 把下面if语句注释掉: #path Manipulation if ["EUID& ...
- iTween
http://u3d.as/content/pixelplacement/i-tween/1s9 download http://itween.pixelplacement.com/documenta ...
- Python科学计算(一)环境简介——Anaconda Python
Anaconda Python 是 Python 科学技术包的合集,功能和 Python(x,y) 类似.它是新起之秀,已更新多次了.包管理使用 conda,GUI基于 PySide,所有的包基本上都 ...
- Windows准备Node.js运行与开发环境
如何在Windows环境下搭建Node.js开发环境:1.下载Node.js windows安装版http://www.nodejs.org/download/ 2.正常安装完成后,在系统环境变量已经 ...