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

Tol*_*Hon 7 php curl

使用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( $ch, CURLINFO_CONTENT_TYPE );
Run Code Online (Sandbox Code Playgroud)

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

Loï*_*oïc 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'
));
Run Code Online (Sandbox Code Playgroud)