Chris Jones
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:
The grabbing:
The foreach http_response_header iteration:
The php.ini:
The result of print_r($http_response_hea der):
Any idea why I'm not getting anything concerned with Gzip returned?
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")
));
The grabbing:
$page = file_get_contents($url, false, $context);
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;
}
The php.ini:
zlib.output_compression = On
The result of print_r($http_response_hea
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 )
Any idea why I'm not getting anything concerned with Gzip returned?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very good and concise. Solution worked and is elegant/sturdy.
ASKER
Thanks again.
ASKER
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?