有没有办法将--compressed传递给PHP的curl_setopt()?

时间:2014-03-03 11:39:25

标签: php curl

使用curl时,我发现服务器上的行为有所不同,具体取决于我是否将--compressed作为参数传递。

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

    curl_setopt( $ch, CURLOPT_ENCODING, 'gzip,deflate,sdch' );

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

但是,如果我通过命令行传递--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 content-type: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( $ch, CURLINFO_CONTENT_TYPE );

无论我尝试什么,我都会获得$ content-type = text/html;charset=UTF-8而不是gzip

2 个答案:

答案 0 :(得分:1)

删除CURLOPT_ENCODING选项(因此curl不会自动解码)并手动将accept-encoding选项放入标题中应该可以解决问题:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding: gzip, deflate',
    'Connection: Close'
));

答案 1 :(得分:1)

并非所有世界各地的服务都提供压缩内容作为回应。通过选项CURLOPT_ENCODING,您只能从服务器请求压缩内容。但是如果没有实现机制,服务器将返回纯HTML。您可以尝试其他提供压缩内容的随机网站。例如youtube.com(可能)。

相关问题