cURL returns 二进制数据而不是 html

cURL returns binary data instead of html

function curl($url) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/25.0.1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, 'long cookie here');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;

}

原来url我喂的是http://example.com/i-123.html but if I open in browser, I get redirected to https://example.com/item-description-123.html(所以加了CURLOPT_FOLLOWLOCATION

但是,这个函数的输出是二进制数据。

1f8b 0800 0000 0000 0003 ed7d e976 db38
f2ef e7f8 2930 9ac9 d86e 9b92 b868 f3a2
3e5e 9374 67fb c7ee 74f7 e4e6 f880 2428
31a6 4835 172f 3dd3 8f74 3fde 17b8 f7c5
6e15 008a 8ba8 2db1 3ce9 25a7 dba4 4810
......

我该如何解决这个问题?我尝试添加

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 

(从某处复制)。没用。

file_get_contents() 给我相同的输出。

好吧,这个解决方案很可怜...

使用 wget -S http://example.com 我发现内容被压缩 (gzipped)。使用 gunzip 我成功提取了 html.

也添加到我原来的 PHP 脚本中

curl_setopt($ch,CURLOPT_ENCODING , "");

而且效果非常好。