Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

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

How can I read a response from Stackoverflow API in PHP? The response is GZIP-ed. I found e.g. the following suggestion:

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

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

Are there some other easy ways to accomplish it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
"))));

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...