Link to home
Start Free TrialLog in
Avatar of Chris Jones
Chris JonesFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Grabbing HTTP_RESPONSE_HEADER array with Content-encoding/Accept-encoding Gzip/Compress value present? (PHP)

Hi all,

I'm trying to use the following code to get header information, ultimately I want to test whether information sent from a URL is Gzipped or not. The code I'm using does not appear to give any information about Gzip, even though the server definitely has Gzip enabled.

The context:
$context = stream_context_create(array('http'=>array('method'=>"GET",'header'=>"Accept: text/html,application/xhtml+xml,application/xml" .
                                                                                               "Accept-Charset: ISO-8859-1,utf-8" .
                                                                                               "Accept-Encoding: gzip,deflate,sdchrn" ,
                            'user_agent'=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
                    ));

Open in new window


The grabbing:
$page = file_get_contents($url, false, $context);

Open in new window


The foreach http_response_header iteration:
 foreach($http_response_header as $c => $h)
                {
                        if(stristr($h, 'content-encoding') and stristr($h, 'gzip'))
                                $content = gzinflate(substr($content,10,-8));
                        echo $content;
                }

Open in new window


The php.ini:
zlib.output_compression = On

Open in new window


The result of print_r($http_response_header):
Array ( [0] => HTTP/1.1 200 OK [1] => Date: Mon, 22 May 2017 12:24:22 GMT [2] => Server: Apache [3] => X-Cache: QuickCache vv2.1.1rc1 - file [4] => ETag: "qcd-19396832.63965" [5] => Vary: Accept-Encoding,User-Agent [6] => Cache-Control: max-age=172800 [7] => Expires: Wed, 24 May 2017 12:24:22 GMT [8] => X-XSS-Protection: 1; mode=block [9] => Connection: close [10] => Content-Type: text/html ) 

Open in new window


Any idea why I'm not getting anything concerned with Gzip returned?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chris Jones

ASKER

Thanks Ray, very good as always.

Do I take it that you would suggest that the use of cURL over file_get_contents()?

Is this even possible with file_get_contents?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very good and concise. Solution worked and is elegant/sturdy.
Thanks again.