php使用curl获取header检测开启GZip压缩的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-10-23 14:32:12 浏览: 评论:0
这篇文章主要介绍了php使用curl获取header检测开启GZip压缩的方法,结合实例形式总结分析了三种常见的header检测方法并给出了使用curl检测Gzip压缩开启情况的方法,需要的朋友可以参考下。
本文实例讲述了php使用curl获取header检测开启GZip压缩的方法,分享给大家供大家参考,具体如下:
获得网页header信息,是网站开发人员和维护人员常用的技术。网页的header信息,非常丰富,非专业人士一般较难读懂和理解各个项目的含义。
获取网页header信息,方法多种多样,就php语言来说,我作为一个菜鸟,知道的方法也有4种那么多。下面逐一献上。
方法一:使用get_headers()函数
这个方法很多人使用,也很简单便捷,只需要两行代码即可搞定。如下:
$thisurl = "https://www.phpfensi.com/";
print_r(get_headers($thisurl, 1));
得到的结果为:
- Array
- (
- [0] => HTTP/1.1 200 OK
- [Content-Type] => text/html
- [Last-Modified] => Wed, 15 Aug 2018 01:23:03 GMT
- [ETag] => "99a921833634d41:0"
- [Server] => Microsoft-IIS/7.5
- [X-Powered-By] => jb51.net
- [Date] => Wed, 15 Aug 2018 01:31:48 GMT
- [Connection] => close
- [Content-Length] => 89251
- )
方法二:使用http_response_header
代码也很简单,仅需三行:
- $thisurl = "https://www.phpfensi.com/";
- $html = file_get_contents($thisurl );
- print_r($http_response_header);
得到的结果为:
- Array
- (
- [0] => HTTP/1.1 200 OK
- [1] => Content-Type: text/html
- [2] => Last-Modified: Wed, 15 Aug 2018 01:33:04 GMT
- [3] => ETag: "7b9757e93734d41:0"
- [4] => Server: Microsoft-IIS/7.5
- [5] => X-Powered-By: jb51.net
- [6] => Date: Wed, 15 Aug 2018 01:34:15 GMT
- [7] => Connection: close
- [8] => Content-Length: 89282
- )
方法三:使用stream_get_meta_data()函数
代码也只有三行:
- $thisurl = "https://www.phpfensi.com/";
- $fp = fopen($thisurl, 'r');
- print_r(stream_get_meta_data($fp));
得到的结果为:
- Array
- (
- [wrapper_data] => Array
- (
- [0] => HTTP/1.1 200 OK
- [1] => Content-Type: text/html
- [2] => Last-Modified: Wed, 15 Aug 2018 01:38:45 GMT
- [3] => ETag: "ecc8f8b43834d41:0"
- [4] => Server: Microsoft-IIS/7.5
- [5] => X-Powered-By: jb51.net
- [6] => Date: Wed, 15 Aug 2018 01:39:35 GMT
- [7] => Connection: close
- [8] => Content-Length: 89421
- )
- [wrapper_type] => http
- [stream_type] => tcp_socket/ssl
- [mode] => r
- [unread_bytes] => 7945
- [seekable] =>
- [uri] => https://www.phpfensi.com/
- [timed_out] =>
- [blocked] => 1
- [eof] =>
- )
上述三种方法都可以轻松获得网页header信息,且包含的信息都已经相当丰富,满足一般要求,不过比较遗憾的是,上述三种方法都不能用来检测网页是否启用了GZip压缩。要检测GZip压缩,还需其他的方法才行。这里介绍的是用curl()函数来检测。
使用curl获得header可以检测GZip压缩
先贴出代码:
- <?php
- $szUrl = 'http://www.webkaka.com/';
- $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("\r\n", "\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.webkaka.com/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
上面输出结果里可以看到一个项目:Content-Encoding: gzip,这个正是我们用来判断网页是否启用GZip压缩的项目。
另外,需要认真注意下本实例里的注释部分,不能少了任何一项,否则可能获取header信息有误。
Tags: curl header GZip
相关文章
- ·php curl常见错误:SSL错误、bool(false)(2013-11-30)
- ·curl out of memory window下PHP调用curl报内存不够(2013-12-06)
- ·windows 下 php curl 的支持配置方法(2013-12-06)
- ·PHP 利用curl_init发起http请求模仿登录(2014-01-06)
- ·php curl 伪造IP来源程序实现代码(2014-01-07)
- ·php curl 分离header和body信息(2014-01-07)
- ·PHP curl 获取响应的状态实例(2014-01-08)
- ·php curl模块模拟登录后采集页面实例(2014-01-08)
- ·PHP Curl多线程实现原理与实例详解(2014-01-09)
- ·Drupal 通过cURL Post方式发送一个文件(2014-01-10)
- ·php 通过curl post发送json数据实例(2014-01-10)
- ·php用Curl伪造客户端源IP(2014-01-10)
- ·php利用CURL函数登入163邮箱并获取自己的通讯录(2014-06-17)
- ·php中CURL实现多线程的笔记(2014-06-18)
- ·PHP利用Curl模拟登录并获取数据例子(2014-06-21)
- ·php中curl模拟登陆用户百度知道的例子(2014-06-29)

推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)