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

怎么解决php gzip css 乱码问题

2026/1/31 19:17:53发布36次查看
php gzip css乱码的解决办法:1、使用自带的zlib库;2、使用curl代替“file_get_contents”;3、使用gzip解压函数解决乱码问题。
本文操作环境:windows7系统、php7.1版,dell g3电脑。
怎么解决php gzip css 乱码问题?
php file_get_contents抓取gzip网页乱码的三种解决方法
用 file_get_contents() 函数抓取网页会发生乱码现象。有两个原因会导致乱码,一个是编码问题,一个是目标页面开了gzip,下面说的就是开了gzip功能如何才能不乱码的方法
把抓取到的内容转下编码即可($content=iconv(gbk, utf-8//ignore, $content);),我们这里讨论的是如何抓取开了gzip的页面。怎么判断呢?获取的头部当中有content-encoding: gzip说明内容是gzip压缩的。用firebug看一下就知道页面开了gzip没有。下面是用firebug查看我的博客的头信息,gzip是开了的。
代码如下:
请求头信息原始头信息
accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8accept-encoding gzip, deflateaccept-language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3connection keep-alivecookie __utma=225240837.787252530.1317310581.1335406161.1335411401.1537; __utmz=225240837.1326850415.887.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=%e4%bb%bb%e4%bd%95%e9%a1%b9%e7%9b%ae%e9%83%bd%e4%b8%8d%e4%bc%9a%e9%82%a3%e4%b9%88%e7%ae%80%e5%8d%95%20site%3awww.nowamagic.net; phpsessid=888mj4425p8s0m7s0frre3ovc7; __utmc=225240837; __utmb=225240837.1.10.1335411401host www.nowamagic.netuser-agent mozilla/5.0 (windows nt 5.1; rv:12.0) gecko/20100101 firefox/12.0
下面介绍一些解决方案:
1. 使用自带的zlib库
如果服务器已经装了zlib库,用下面的代码可以轻易解决乱码问题。
代码如下:
$data = file_get_contents("compress.zlib://".$url);
2. 使用curl代替file_get_contents
代码如下:
function curl_get($url, $gzip=false){ $curl = curl_init($url); curl_setopt($curl, curlopt_returntransfer, 1); curl_setopt($curl, curlopt_connecttimeout, 10); if($gzip) curl_setopt($curl, curlopt_encoding, "gzip"); // 关键在这里 $content = curl_exec($curl); curl_close($curl); return $content;}
3. 使用gzip解压函数
代码如下:
function gzdecode($data) { $len = strlen($data); if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) { return null; // not gzip format (see rfc 1952) } $method = ord(substr($data,2,1)); // compression method $flags = ord(substr($data,3,1)); // flags if ($flags & 31 != $flags) { // reserved bits are set -- not allowed by rfc 1952 return null; } // note: $mtime may be negative (php integer limitations) $mtime = unpack("v", substr($data,4,4)); $mtime = $mtime[1]; $xfl = substr($data,8,1); $os = substr($data,8,1); $headerlen = 10; $extralen = 0; $extra = ""; if ($flags & 4) { // 2-byte length prefixed extra data in header if ($len - $headerlen - 2 < 8) { return false; // invalid format } $extralen = unpack("v",substr($data,8,2)); $extralen = $extralen[1]; if ($len - $headerlen - 2 - $extralen < 8) { return false; // invalid format } $extra = substr($data,10,$extralen); $headerlen += 2 + $extralen; } $filenamelen = 0; $filename = ""; if ($flags & 8) { // c-style string file name data in header if ($len - $headerlen - 1 < 8) { return false; // invalid format } $filenamelen = strpos(substr($data,8+$extralen),chr(0)); if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) { return false; // invalid format } $filename = substr($data,$headerlen,$filenamelen); $headerlen += $filenamelen + 1; } $commentlen = 0; $comment = ""; if ($flags & 16) { // c-style string comment data in header if ($len - $headerlen - 1 < 8) { return false; // invalid format } $commentlen = strpos(substr($data,8+$extralen+$filenamelen),chr(0)); if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) { return false; // invalid header format } $comment = substr($data,$headerlen,$commentlen); $headerlen += $commentlen + 1; } $headercrc = ""; if ($flags & 1) { // 2-bytes (lowest order) of crc32 on header present if ($len - $headerlen - 2 < 8) { return false; // invalid format } $calccrc = crc32(substr($data,0,$headerlen)) & 0xffff; $headercrc = unpack("v", substr($data,$headerlen,2)); $headercrc = $headercrc[1]; if ($headercrc != $calccrc) { return false; // bad header crc } $headerlen += 2; } // gzip footer - these be negative due to php's limitations $datacrc = unpack("v",substr($data,-8,4)); $datacrc = $datacrc[1]; $isize = unpack("v",substr($data,-4)); $isize = $isize[1]; // perform the decompression: $bodylen = $len-$headerlen-8; if ($bodylen < 1) { // this should never happen - implementation bug! return null; } $body = substr($data,$headerlen,$bodylen); $data = ""; if ($bodylen > 0) { switch ($method) { case 8: // currently the only supported compression method: $data = gzinflate($body); break; default: // unknown compression method return false; } } else { // i'm not sure if zero-byte body content is allowed. // allow it for now... do nothing... } // verifiy decompressed size and crc32: // note: this may fail with large data sizes depending on how // php's integer limitations affect strlen() since $isize // may be negative for large sizes. if ($isize != strlen($data) || crc32($data) != $datacrc) { // bad format! length or crc doesn't match! return false; } return $data;}
使用:
代码如下:
$html=file_get_contents('https://www.jb51.net/');$html=gzdecode($html);
就介绍这三个方法,应该能解决大部分gzip引起的抓取乱码问题了。
推荐学习:《php视频教程》
以上就是怎么解决php gzip css 乱码问题的详细内容。
该用户其它信息

VIP推荐

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