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
curl allows you to ask HTTP and HTTPS servers to provide compressed versions of the data and then perform automatic decompression of it on arrival. In situations where bandwidth is more limited than CPU this will help you receive more data in a shorter amount of time.
The curl_setopt() function will set options for a CURL session identified by the ch parameter. The option parameter is the option you want to set, and the value is the value of the option given by the option. The value should be a long for the following options (specified in the option parameter):
CURLOPT_RETURNTRANSFER. true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly. CURLOPT_SASL_IR. true to enable sending the initial response in the first packet. Added in cURL 7.31.
Removing the CURLOPT_ENCODING
option (so curl doesn't auto-decode) and putting the accept-encoding
option in the header manually should do the trick :
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'
));
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With