欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php file_get_contents抓取Gzip网页乱码的三种解决方法

程序员文章站 2022-03-12 18:44:30
把抓取到的内容转下编码即可($content=iconv("gbk", "utf-8//ignore", $content);),我们这里讨论的是如何抓取开了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.8
accept-encoding gzip, deflate
accept-language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
connection keep-alive
cookie __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.1335411401
host www.nowamagic.net
user-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('//www.jb51.net/');
$html=gzdecode($html);

就介绍这三个方法,应该能解决大部分gzip引起的抓取乱码问题了。