Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass --compressed into PHP's curl_setopt()?

Tags:

php

curl

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

like image 845
Toland Hon Avatar asked Mar 03 '14 11:03

Toland Hon


People also ask

What is compressed in curl?

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.

What is the use of curl_setopt?

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):

What is Curlopt_returntransfer in curl?

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.


2 Answers

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'
));
like image 189
Loïc Avatar answered Nov 09 '22 20:11

Loïc


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).

like image 36
Sabuj Hassan Avatar answered Nov 09 '22 20:11

Sabuj Hassan