How to Read Gzip-Ed Response from Stackoverflow API in PHP

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

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

Notice the use of a stream wrapper, 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 removed because they don't send an Accept-Encoding http header.

How do I use file_get_contents to get a gzip'ed page on a remote web server in php?

Have you tried this?

$content = file_get_contents('http://example.com',false,$context);

StackOverflow API and PHP CLI and JSON

You need to un gzip it even if you didn't send an accept: gzip header in the http request, because all responses are gzip'd

see
How can I read GZIP-ed response from Stackoverflow API in PHP?

Getting null as response from Stack Overflow API with PHP

The problem is that the response is also gzipped.

My preferred fix would be to use curl, with CURLOPT_ENCODING option.

<?php
function curl($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_USERAGENT, 'cURL');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_ENCODING , "gzip");//<< Solution

$result = curl_exec($curl);
curl_close($curl);

return $result;
}

$feed = curl("http://api.stackexchange.com/2.1/users/22656?order=desc&sort=reputation&site=stackoverflow&filter=!*MxOyD8qN0Yghnep");

$feed = json_decode($feed,true);
$rep = $feed['items'][0]['reputation'];
echo $rep;//531776
?>

Though, you can use normal FGC, then inflate the response back into uncompressed.

<?php
$feed = file_get_contents('http://api.stackexchange.com/2.1/users/22656?order=desc&sort=reputation&site=stackoverflow&filter=!*MxOyD8qN0Yghnep');
$feed = gzinflate(substr($feed, 10, -8));

$feed = json_decode($feed,true);
$rep = $feed['items'][0]['reputation'];
echo $rep;//531776
?>

Decode GZIP stream in PHP from C#

Curious one, I usually look up WordPress core for handling these things, since they got most of edge cases caught over years. Not this one though.

Rather than trying to rebuild that C# code I have simply brute forced through multiple combinations of related functions and offsets.

This got it done:

var_dump( gzdecode( substr( base64_decode( $data ), 4 ) ) );

// string(128) "{"Code":412,"Value":"RegistrationSet","Route":"CN23/C_Document"}"

Why file_get_contents returning garbled data?

Your site response is being compressed, therefore you've to uncompress in order to convert it to the original form.

The quickest way is to use gzinflate() as below:

$html = gzinflate(substr(file_get_contents("https://kat.cr/usearch/architecture%20category%3Abooks/"), 10, -8));

Or for more advanced solution, please consider the following function (found at this blog):

function get_url($url)
{
//user agent is very necessary, otherwise some websites like google.com wont give zipped content
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-Language: en-US,en;q=0.8rn" .
"Accept-Encoding: gzip,deflate,sdchrn" .
"Accept-Charset:UTF-8,*;q=0.5rn" .
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn"
)
);

$context = stream_context_create($opts);
$content = file_get_contents($url ,false,$context);

//If http response header mentions that content is gzipped, then uncompress it
foreach($http_response_header as $c => $h)
{
if(stristr($h, 'content-encoding') and stristr($h, 'gzip'))
{
//Now lets uncompress the compressed data
$content = gzinflate( substr($content,10,-8) );
}
}

return $content;
}

echo get_url('http://www.google.com/');

Uncompress a gzip file from CURL, on php

Use gzdecode:

<?php
$c = file_get_contents("http://torcache.com/" .
"torrent/63ABC1435AA5CD48DCD866C6F7D5E80766034391.torrent");
echo gzdecode($c);

gives


d8:announce42:http://tracker.openbittorrent.com/announce13:announce-listll42
...


Related Topics



Leave a reply



Submit