把抓取到的内容转下编码即可($content=iconv("GBK","UTF-8//IGNORE",$content);),我们这里讨论的是如何抓取开了Gzip的页面。怎么判断呢?获取的头部当中有Content-Encoding: gzip说明内容是GZIP压缩的。用FireBug看一下就知道页面开了gzip没有。下面是用firebug查看我的博客的头信息,Gzip是开了的。@H_301_1@
1. 使用自带的zlib库@H_301_1@
如果服务器已经装了zlib库,用下面的代码可以轻易解决乱码问题。@H_301_1@2. 使用CURL代替file_get_contents@H_301_1@
3. 使用gzip解压函数@H_301_1@
$filenamelen = 0; @H_301_1@ $filename = ""; @H_301_1@ if ($flags & 8) { @H_301_1@ // C-style string file NAME data in header @H_301_1@ if ($len - $headerlen - 1 < 8) { @H_301_1@ return false; // Invalid format @H_301_1@ } @H_301_1@ $filenamelen = strpos(substr($data,8+$extralen),chr(0)); @H_301_1@ if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) { @H_301_1@ return false; // Invalid format @H_301_1@ } @H_301_1@ $filename = substr($data,$headerlen,$filenamelen); @H_301_1@ $headerlen += $filenamelen + 1; @H_301_1@ }
$commentlen = 0; @H_301_1@ $comment = ""; @H_301_1@ if ($flags & 16) { @H_301_1@ // C-style string COMMENT data in header @H_301_1@ if ($len - $headerlen - 1 < 8) { @H_301_1@ return false; // Invalid format @H_301_1@ } @H_301_1@ $commentlen = strpos(substr($data,8+$extralen+$filenamelen),chr(0)); @H_301_1@ if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) { @H_301_1@ return false; // Invalid header format @H_301_1@ } @H_301_1@ $comment = substr($data,$commentlen); @H_301_1@ $headerlen += $commentlen + 1; @H_301_1@ }
$headercrc = ""; @H_301_1@ if ($flags & 1) { @H_301_1@ // 2-bytes (lowest order) of CRC32 on header present @H_301_1@ if ($len - $headerlen - 2 < 8) { @H_301_1@ return false; // Invalid format @H_301_1@ } @H_301_1@ $calccrc = crc32(substr($data,$headerlen)) & 0xffff; @H_301_1@ $headercrc = unpack("v",2)); @H_301_1@ $headercrc = $headercrc[1]; @H_301_1@ if ($headercrc != $calccrc) { @H_301_1@ return false; // Bad header CRC @H_301_1@ } @H_301_1@ $headerlen += 2; @H_301_1@ }
// GZIP FOOTER - These be negative due to PHP's limitations @H_301_1@ $datacrc = unpack("V",-8,4)); @H_301_1@ $datacrc = $datacrc[1]; @H_301_1@ $isize = unpack("V",-4)); @H_301_1@ $isize = $isize[1];
// Perform the decompression: @H_301_1@ $bodylen = $len-$headerlen-8; @H_301_1@ if ($bodylen < 1) { @H_301_1@ // This should never happen - IMPLEMENTATION BUG! @H_301_1@ return null; @H_301_1@ } @H_301_1@ $body = substr($data,$bodylen); @H_301_1@ $data = ""; @H_301_1@ if ($bodylen > 0) { @H_301_1@ switch ($method) { @H_301_1@ case 8: @H_301_1@ // Currently the only supported compression method: @H_301_1@ $data = gzinflate($body); @H_301_1@ break; @H_301_1@ default: @H_301_1@ // Unknown compression method @H_301_1@ return false; @H_301_1@ } @H_301_1@ } else { @H_301_1@ // I'm not sure if zero-byte body content is allowed. @H_301_1@ // Allow it for now... Do nothing... @H_301_1@ }
// Verifiy decompressed size and CRC32: @H_301_1@ // NOTE: This may fail with large data sizes depending on how @H_301_1@ // PHP's integer limitations affect strlen() since $isize @H_301_1@ // may be negative for large sizes. @H_301_1@ if ($isize != strlen($data) || crc32($data) != $datacrc) { @H_301_1@ // Bad format! Length or CRC doesn't match! @H_301_1@ return false; @H_301_1@ } @H_301_1@ return $data; @H_301_1@}@H_301_1@