php使用curl获取header检测开启GZip压缩的方法


Posted in PHP onAugust 15, 2018

本文实例讲述了php使用curl获取header检测开启GZip压缩的方法。分享给大家供大家参考,具体如下:

获得网页header信息,是网站开发人员和维护人员常用的技术。网页的header信息,非常丰富,非专业人士一般较难读懂和理解各个项目的含义。

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

方法一:使用get_headers()函数

这个方法很多人使用,也很简单便捷,只需要两行代码即可搞定。如下:

$thisurl = "https://3water.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] => 3water.com
    [Date] => Wed, 15 Aug 2018 01:31:48 GMT
    [Connection] => close
    [Content-Length] => 89251
)

方法二:使用http_response_header

代码也很简单,仅需三行:

$thisurl = "https://3water.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: 3water.com
    [6] => Date: Wed, 15 Aug 2018 01:34:15 GMT
    [7] => Connection: close
    [8] => Content-Length: 89282
)

方法三:使用stream_get_meta_data()函数

代码也只有三行:

$thisurl = "https://3water.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: 3water.com
            [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://3water.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信息有误。

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
PHP 引用是个坏习惯
Mar 12 PHP
PHP获取url的函数代码
Aug 02 PHP
PHP 获取文件路径(灵活应用__FILE__)
Feb 15 PHP
如何解决CI框架的Disallowed Key Characters错误提示
Jul 05 PHP
解析php mysql 事务处理回滚操作(附实例)
Aug 05 PHP
PHP error_log()将错误信息写入一个文件(定义和用法)
Oct 25 PHP
PHP实现加密的几种方式介绍
Feb 22 PHP
php分割合并两个字符串的函数实例
Jun 19 PHP
ThinkPHP控制器详解
Jul 27 PHP
php原生导出excel文件的两种方法(推荐)
Nov 19 PHP
php json_encode与json_decode详解及实例
Dec 13 PHP
php empty 函数判断结果为空但实际值却为非空的原因解析
May 28 PHP
深入研究PHP中的preg_replace和代码执行
Aug 15 #PHP
PHP中一个有趣的preg_replace函数详解
Aug 15 #PHP
PHP使用curl_multi_select解决curl_multi网页假死问题的方法
Aug 15 #PHP
php+croppic.js实现剪切上传图片功能
Aug 14 #PHP
PHP设计模式之委托模式定义与用法简单示例
Aug 13 #PHP
PHP设计模式之建造者模式定义与用法简单示例
Aug 13 #PHP
PHP设计模式之装饰器模式定义与用法简单示例
Aug 13 #PHP
You might like
建立动态的WML站点(二)
2006/10/09 PHP
php防止sql注入代码实例
2013/12/18 PHP
常见php数据文件缓存类汇总
2014/12/05 PHP
php mysqli查询语句返回值类型实例分析
2016/06/29 PHP
use jscript Create a SQL Server database
2007/06/16 Javascript
jQuery中closest()函数用法实例
2015/01/07 Javascript
JavaScript中Object.prototype.toString方法的原理
2016/02/24 Javascript
详解Vue2.0里过滤器容易踩到的坑
2017/06/01 Javascript
从源码看angular/material2 中 dialog模块的实现方法
2017/10/18 Javascript
解决vue多个路由共用一个页面的问题
2018/03/12 Javascript
js实现前面自动补全位数的方法
2018/10/10 Javascript
详解如何在vscode里面调试js和node.js的方法步骤
2018/12/24 Javascript
微信小程序生成二维码的示例代码
2019/03/29 Javascript
Layui 带多选框表格监听事件以及按钮自动点击写法实例
2019/09/02 Javascript
[01:47]2018年度DOTA2最具人气解说-完美盛典
2018/12/16 DOTA
使用Python脚本将文字转换为图片的实例分享
2015/08/29 Python
python爬取足球直播吧五大联赛积分榜
2018/06/13 Python
python从list列表中选出一个数和其对应的坐标方法
2019/07/20 Python
pandas DataFrame的修改方法(值、列、索引)
2019/08/02 Python
python3 反射的四种基本方法解析
2019/08/26 Python
Python实现列表中非负数保留,负数转化为指定的数值方式
2020/06/04 Python
浅谈matplotlib 绘制梯度下降求解过程
2020/07/12 Python
关于HTML5的安全问题开发人员需要牢记的
2012/06/21 HTML / CSS
瑞士灯具购物网站:Lampenwelt.ch
2018/07/08 全球购物
越南综合购物网站:Lazada越南
2019/06/10 全球购物
DC Shoes荷兰官方网站:美国极限运动品牌
2019/10/22 全球购物
致短跑运动员广播稿
2014/01/09 职场文书
家长给学校的建议书
2014/05/15 职场文书
少先队活动总结
2014/08/29 职场文书
2015元旦主持词开场白和结束语
2014/12/14 职场文书
2014个人年度工作总结
2014/12/15 职场文书
2014年个人总结范文
2015/03/09 职场文书
2015年污水处理厂工作总结
2015/05/26 职场文书
荒岛余生观后感
2015/06/09 职场文书
2016年教师师德师风心得体会
2016/01/12 职场文书
python绘制箱型图
2021/04/27 Python