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

后端 未结 1 1783
盖世英雄少女心
盖世英雄少女心 2020-11-28 16:08

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.co         


        
相关标签:
1条回答
  • 2020-11-28 16:23

    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.

    0 讨论(0)
提交回复
热议问题