有没有办法传递 - 压缩到PHP的curl_setopt()? [英] Is there a way to pass --compressed into PHP's curl_setopt()?

查看:164
本文介绍了有没有办法传递 - 压缩到PHP的curl_setopt()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用curl时,如果我通过 - 压缩作为参数,我会看到服务器上的行为有所不同。 / p>

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

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

我也尝试将编码设置为空字符串:'',因为这应该意味着任何类型的压缩



但是,如果我通过命令行传递 - 压缩,内容类型I回到的是: gzip 。如果我不传递 - 压缩,我回来的内容类型是 text / html; charset = UTF-8



使用PHP的 curl_exec(),我似乎不能得到它返回一个内容-type:gzip。



=====



让我澄清我要完成的事。当我运行以下命令: curl -I http://someserver.com --compressed 获取我content-type: gzip



运行相同的命令 curl -I http://someserver.com c> - compressed get me
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);

//我已经尝试排除这一行,将其设置为gzip和空字符串
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

解决方案

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


When using curl, I'm seeing a difference in behaviors on the server I'm hitting depending on if I passed --compressed as an argument or not.

I'm already setting the Accept-Encoding header to gzip,deflate,sdch:

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

I've also tried setting the encoding to empty string: '' since that supposedly means any type compression is supported.

However, if I've pass --compressed via the command line, the content type I get back is: gzip. If I don't pass in --compressed, the content type I get back is text/html;charset=UTF-8

Using PHP's curl_exec(), I can't ever seem to get it to return a content-type: gzip.

=====

Let me clarify what I'm trying to accomplish. When I run the following command: curl -I http://someserver.com --compressed gets me content-type: gzip

Running the same command curl -I http://someserver.com without --compressed gets me content-type: text/html;charset=UTF-8

Trying to do this in 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 );

No matter what I try, I'm getting $content-type = text/html;charset=UTF-8 and not gzip

解决方案

Not all serves around the world provide the compressed content as a response. Through the option CURLOPT_ENCODING you can only ask for compressed content from the server. But server will return you plain html if it has not implemented the mechanism. You can try with other random websites which provides compress content. For example youtube.com(probably).

这篇关于有没有办法传递 - 压缩到PHP的curl_setopt()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆