【问题标题】:Uncompress a gzip file from CURL, on php在 php 上从 CURL 解压缩 gzip 文件
【发布时间】:2019-12-10 06:45:51
【问题描述】:

有人知道如何解压缩我使用 curl 获得的 gzip 文件的内容吗?

例如:http://torcache.com/torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent

回复

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 09 Jun 2010 01:11:26 GMT
Content-Type: application/x-bittorrent
Content-Length: 52712
Last-Modified: Tue, 08 Jun 2010 15:09:58 GMT
Connection: keep-alive
Expires: Fri, 09 Jul 2010 01:11:26 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip
Accept-Ranges: bytes

然后是压缩的gzip,

我试过gzdecode但是不行,gzeflate也一样,他们根本没有得到任何响应,文件的内容不超过2k

【问题讨论】:

    标签: php curl gzip gunzip


    【解决方案1】:

    只要告诉 cURL 在压缩后自动解码响应即可

    curl_setopt($ch,CURLOPT_ENCODING, '');
    

    【讨论】:

    • 值应该是空字符串或编码类型。不是int
    • 谢谢!这不是一个合理的默认 CURL 设置吗?
    • @redben My proposal 在 2011 年被拒绝,现在 AlixAxel's 获得批准。 :(
    【解决方案2】:

    使用gzdecode:

    <?php
        $c = file_get_contents("http://torcache.com/" .
            "torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
        echo gzdecode($c);
    

    给予

    d8:announce42:http://tracker.openbittorrent.com/announce13:announce-listll42 ...

    【讨论】:

    • 是的,但我有 php 5.2,他们说 gzdecode 从 6.0 开始可用,只有我有一个配置了 php 和 zlib 的 gentoo,我似乎无法使用这些 gz 功能。有什么想法吗? :)
    • 好的,我有一个解决方案,不漂亮,因为我必须将内容写入文件,而不仅仅是使用字符串:function gzdecode($data){ $g=tempnam('/tmp ','ff'); @file_put_contents($g,$data); ob_start();读取gzfile($g); $d=ob_get_clean();取消链接($g);返回$d; } 并且它有效:)
    • @PartySoft 这个功能太棒了!
    • @PartySoft 谢谢!像魅力一样工作。运行 5.3.X 并没有遇到gzuncompress()
    • 什么鬼,不,只需将 CURLOPT_ENCODING 设置为“gzip”,curl 就会自动解压缩 gzip 编码的响应
    【解决方案3】:

    libcurl 提供了一项功能,可以自动解压缩内容(如果使用 zlib 构建)。

    查看 CURLOPT_ACCEPT_ENCODING 选项:https://curl.haxx.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html

    【讨论】:

      【解决方案4】:

      您是否尝试过如下设置标头以表明您接受 gzip 编码?:

      curl_setopt($rCurl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
      

      【讨论】:

      • 他已经收到了它的编码,所以我认为声明他接受它的编码不会有任何区别。我尝试向服务器发送 Accept-encoding: none 并且服务器不遵守。
      【解决方案5】:

      zlib Stream Wrapper:

      file_get_contents("compress.zlib://http://torcache.com/" .
          "torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
      

      【讨论】:

        【解决方案6】:

        你试过gzuncompressgzinflate吗?

        gzdeflate 压缩,与您想要的相反。老实说,我无法弄清楚gzdecode 与普通解压缩有何不同。

        还有 cURL 选项CURLOPT_ENCODING:

        “Accept-Encoding:”标头的内容。 这可以对响应进行解码。支持的编码有“identity”、“deflate”和“gzip”。如果设置了空字符串“”,则会发送包含所有支持的编码类型的标头。

        好像是说会自动解压响应,不过我没测试过。

        【讨论】:

          【解决方案7】:

          您可以使用 gzinflate 来实现(假设 $headers 包含您的所有 HTTP 标头,而 $buffer 包含您的数据):

          if (isset($headers['Content-Encoding']) && ($headers['Content-Encoding'] === 'gzip' || $headers['Content-Encoding'] === 'deflate'))
              {
                  if ($headers['Content-Encoding'] === 'gzip')
                  {
                      $buffer = substr($buffer, 10);
                  }
                  $contents = @gzinflate($buffer);
                  if ($contents === false)
                  {
                      return false;
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-02-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-07-06
            相关资源
            最近更新 更多