相关疑难解决方法(0)

如何在使用curl时正确处理gzip压缩页面?

我编写了一个bash脚本,它使用curl从一个网站输出,并在html输出上做了一堆字符串操作.问题是当我针对返回其输出gzip的网站运行它时.在浏览器中访问该网站工作正常.

当我手动运行curl时,我得到gzipped输出:

$ curl "http://example.com"
Run Code Online (Sandbox Code Playgroud)

这是该特定网站的标题:

HTTP/1.1 200 OK
Server: nginx
Content-Type: text/html; charset=utf-8
X-Powered-By: PHP/5.2.17
Last-Modified: Sat, 03 Dec 2011 00:07:57 GMT
ETag: "6c38e1154f32dbd9ba211db8ad189b27"
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: must-revalidate
Content-Encoding: gzip
Content-Length: 7796
Date: Sat, 03 Dec 2011 00:46:22 GMT
X-Varnish: 1509870407 1509810501
Age: 504
Via: 1.1 varnish
Connection: keep-alive
X-Cache-Svr: p2137050.pubip.peer1.net
X-Cache: HIT
X-Cache-Hits: 425
Run Code Online (Sandbox Code Playgroud)

我知道返回的数据是gzip压缩的,因为这会返回html,如预期的那样:

$ curl "http://example.com" | gunzip
Run Code Online (Sandbox Code Playgroud)

我不想通过gunzip管道输出,因为脚本在其他站点上按原样运行,并且通过gzip管道将破坏该功能.

我试过的

  1. 更改用户代理(我尝试了我的浏览器发送的相同字符串,"Mozilla/4.0"等)
  2. 男人卷曲
  3. 谷歌搜索
  4. 搜索stackoverflow

一切都空了

有任何想法吗?

bash curl gzip

123
推荐指数
1
解决办法
7万
查看次数

有没有办法将--compressed压缩到PHP的curl_setopt()中?

使用curl时,我看到的是在服务器上的行为差异,这取决于我是否将其--compressed作为参数传递。

我已经将Accept-Encoding标头设置为gzip,deflate,sdch:

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

我还尝试将编码设置为空字符串:”,因为这意味着支持任何类型的压缩。

但是,如果我已--compressed通过命令行传递,则返回的内容类型为:gzip。如果我不传递--compressed,则返回的内容类型为text/html;charset=UTF-8

使用PHP的curl_exec(),我似乎无法获得返回内容类型的文件:gzip。

=====

让我澄清一下我要完成的工作。当我运行以下命令时:curl -I http://someserver.com --compressed获取内容类型:gzip

运行相同的命令curl -I http://someserver.com而不--compressed获取内容类型:text/html;charset=UTF-8

尝试在PHP中执行此操作:

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://someserver.com" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

// I've tried excluding this line, setting it to gzip, and empty string
curl_setopt( $ch, CURLOPT_ENCODING, '' ); 
curl_setopt( $ch, CURLOPT_HEADER, 1);

curl_exec( $ch ) );
$content_type = curl_getinfo( …
Run Code Online (Sandbox Code Playgroud)

php curl

7
推荐指数
1
解决办法
5363
查看次数

标签 统计

curl ×2

bash ×1

gzip ×1

php ×1