解码通过 PHP 中的 cURL 检索的 gzipped 网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/310650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:20:40  来源:igfitidea点击:

Decode gzipped web page retrieved via cURL in PHP

phpencodinggzipdecoding

提问by Ian

I'm retrieving a gzipped web page via curl, but when I output the retrieved content to the browser I just get the raw gzipped data. How can I decode the data in PHP?

我正在通过 curl 检索 gzipped 网页,但是当我将检索到的内容输出到浏览器时,我只获得原始 gzipped 数据。如何在 PHP 中解码数据?

One method I found was to write the content to a tmp file and then ...

我发现的一种方法是将内容写入 tmp 文件,然后...

$f = gzopen($filename,"r");
$content = gzread($filename,250000);
gzclose($f);

.... but man, there's got to be a better way.

....但是伙计,必须有更好的方法。

Edit: This isn't a file, but a gzipped html page returned by a web server.

编辑:这不是一个文件,而是一个由 web 服务器返回的 gzipped html 页面。

回答by jonasl

I use curl and:

我使用 curl 并且:

curl_setopt($ch,CURLOPT_ENCODING , "gzip");

回答by Maryam Jeddian

Versatile GUNZIP function:

多功能 GUNZIP 功能:

   function gunzip($zipped) {
      $offset = 0;
      if (substr($zipped,0,2) == "\x1f\x8b")
         $offset = 2;
      if (substr($zipped,$offset,1) == "\x08")  {
         # file_put_contents("tmp.gz", substr($zipped, $offset - 2));
         return gzinflate(substr($zipped, $offset + 8));
      }
      return "Unknown Format";
   }  

Example of integrating function with CURL:

将函数与 CURL 集成的示例:

      $headers_enabled = 1;
      curl_setopt($c, CURLOPT_HEADER,  $headers_enabled)
      $ret = curl_exec($c);

      if ($headers_enabled) {
         # file_put_contents("preungzip.html", $ret);

         $sections = explode("\x0d\x0a\x0d\x0a", $ret, 2);
         while (!strncmp($sections[1], 'HTTP/', 5)) {
            $sections = explode("\x0d\x0a\x0d\x0a", $sections[1], 2);
         }
         $headers = $sections[0];
         $data = $sections[1];

         if (preg_match('/^Content-Encoding: gzip/mi', $headers)) {
            printf("gzip header found\n");
            return gunzip($data);
         }
      }

      return $ret;

回答by Oddthinking

There are several solutions proposed in the comments on the PHP page for gzdecode.

PHP 页面上的 gzdecode评论中提出了几种解决方案。