在PHP上解压缩来自CURL的gzip文件

Par*_*oft 30 php curl gzip gunzip

有谁知道如何解压缩我用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
Run Code Online (Sandbox Code Playgroud)

那么压缩的gzip,

我试过gzdecode但是不起作用,gzeflate也不是他们根本没有得到任何回复,文件的内容不超过2k

mix*_*dev 76

只需告诉cURL,只要它被压缩,就会自动解码响应

curl_setopt($ch,CURLOPT_ENCODING, '');
Run Code Online (Sandbox Code Playgroud)

  • 值应为空字符串或编码类型.不是int (3认同)

Art*_*cto 14

用途gzdecode:

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

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

  • 好吧,我有一个解决方案,没有漂亮,因为我必须写一个文件的内容,而不仅仅使用一个字符串:function gzdecode($ data){$ g = tempnam('/ tmp','ff'); @file_put_contents($克,$数据); ob_start(); readgzfile($克); $ d = ob_get_clean(); 取消关联($克); 返回$ d; 它工作:) (6认同)
  • 是的,但我有PHP 5.2,并且他们说gzdecode从6.0开始只有我有一个配置了php和zlib的gentoo,我似乎无法使用那些gz函数.任何想法?:) (3认同)

Dan*_*erg 12

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

请参阅CURLOPT_ENCODING选项:http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTENCODING