首页 » PHP教程 » php检测是https技巧_php判断网页是否经由GZIP压缩

php检测是https技巧_php判断网页是否经由GZIP压缩

访客 2024-11-16 0

扫一扫用手机浏览

文章目录 [+]

编写php函数

<?phpfunction IsGzip($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_ENCODING, ''); $response = curl_exec($ch); if(!curl_errno($ch)){ $info = curl_getinfo($ch); $header_size = $info['header_size']; $header_str = substr($response, 0, $header_size); $filter = array(\"大众\r\n\公众, \"大众\r\公众); $header_str = str_replace($filter, PHP_EOL, $header_str); preg_match('/Content-Encoding: (.)\s/i', $header_str, $matches); if(isset($matches[1]) && $matches[1]=='gzip'){ return true; } } return false;}?>

调用函数代码

php检测是https技巧_php判断网页是否经由GZIP压缩

<?php$url = 'https://www.feiniaomy.com';if(IsGzip($url)){ echo $url.' 经由GZIP压缩';}else{ echo $url.' 未经由GZIP压缩';}?>

输出结果:

php检测是https技巧_php判断网页是否经由GZIP压缩
(图片来自网络侵删)

https://www.feiniaomy.com 经由GZIP压缩

php 利用 get_headers() 函数来检测网页是否经由GZIP压缩

get_headers() 是php系统级函数,他返回一个包含有做事器相应一个 HTTP 要求所发送的标头的数组,我们可以检测数据中是否含有 Vary 元素,以及 Vary 元素的值是否为 Accept-Encoding ,来检测网页文件是否进行GZIP压缩

编写php函数

<?phpfunction IsGzip($url){ $header = get_headers($url, 1); if(isset($header['Vary']) && $header['Vary']=='Accept-Encoding'){ return true; } return false;}?>

函数调用的方法

<?php$url = 'https://www.baidu.com';if(IsGzip($url)){ echo $url.' 经由GZIP压缩';}else{ echo $url.' 未经由GZIP压缩';}?>

输出结果:

https://www.baidu.com 经由GZIP压缩

标签:

相关文章

介绍白点控制之路,从原理到方法

白点,作为生活中常见的现象,无处不在。对于如何控制白点,许多人却感到困惑。本文将从原理出发,探讨白点的控制方法,并结合实际案例,为...

PHP教程 2025-01-03 阅读1 评论0

介绍直播王者,如何开启你的电竞直播之旅

随着电竞产业的蓬勃发展,越来越多的年轻人投身于电竞直播行业。王者荣耀作为一款备受欢迎的MOBA手游,吸引了大量玩家和观众。如何开启...

PHP教程 2025-01-03 阅读1 评论0