How can I read the GZIP-ed answer from the Stackoverflow API in PHP?

How can I read the response of the Stackoverflow API in PHP? The answer is GZIP-ed. I found for example. following sentence:

$url = "http://api.stackoverflow.com/1.1/questions/" . $question_id; $data = file_get_contents($url); $data = http_inflate($data); 

but the http_inflate() function is not available during the installation that I am using.

Are there any other ways to do this?

+7
source share
1 answer

Great way http://www.php.net/manual/en/wrappers.compression.php

Pay attention to the use of the stream fairing, compress.zlib

 $url = "compress.zlib://http://api.stackoverflow.com/1.1/questions/" . $question_id; echo $data = file_get_contents($url, false, stream_context_create(array('http'=>array('header'=>"Accept-Encoding: gzip\r\n")))); 

or using curl

 $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => $url , CURLOPT_HEADER => 0 , CURLOPT_RETURNTRANSFER => 1 , CURLOPT_ENCODING => 'gzip' )); echo curl_exec($ch); 

edited-- other methods are deleted because they do not send an Accept-Encoding http header.

+18
source

All Articles