您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 实现判断页面或图片是否经过gzip压缩的方法

2024/6/11 9:24:14发布11次查看
下面小编就为大家带来一篇php 判断页面或图片是否经过gzip压缩的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
使用php判断页面或图片是否经过gzip压缩方法
1.使用get_headers
页面内容
<?php ob_start('ob_gzhandler'); // 开启gzip,屏蔽则关闭 $data = array( array('name'=>'one','value'=>1), array('name'=>'two','value'=>2), array('name'=>'three','value'=>3) ); header('content-type:application/json'); echo json_encode($data); ?>
使用get_headers判断是否使用gzip压缩
<?php $url = 'http://www.example.com/'; var_dump(check_gzip($url)); /** * 判断url是否经过gzip压缩 * @param string $url 来源 * @param boolean */ function check_gzip($url){ $header = get_headers($url, 1); if(isset($header['vary']) && $header['vary']=='accept-encoding'){ return true; } return false; } ?>
测试结果:
当加上ob_gzhandler时,返回true,删除后返回false
2.使用curl
图片
<?php header('content-type:image/jpeg'); ob_start('ob_gzhandler'); // 开启gzip,屏蔽则关闭 echo file_get_contents('test.jpg'); ?>
使用curl判断是否使用gzip压缩
<?php $url = 'http://www.example.com/'; var_dump(check_gzip($url)); /** * 判断url是否经过gzip压缩 * @param string $url 来源 * @param boolean */ function check_gzip($url){ $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_header, 1); // 输出header信息 curl_setopt($ch, curlopt_returntransfer, 1); // 返回的信息不直接输出 curl_setopt($ch, curlopt_encoding, ''); // 不限制编码类型 $response = curl_exec($ch); if(!curl_errno($ch)){ $info = curl_getinfo($ch); // 获取header $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); // 检查content-encoding preg_match('/content-encoding: (.*)\s/i', $header_str, $matches); if(isset($matches[1]) && $matches[1]=='gzip'){ return true; } } return false; } ?>
测试结果:
当加上ob_gzhandler时,返回true,删除后返回false
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
php函数将任意数字转换成对应的汉字数字
php中的protected的引发的思考
php之正则表达式捕获组与非捕获组
以上就是php 实现判断页面或图片是否经过gzip压缩的方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product