gpt4 book ai didi

php - 如何在 PHP 中读取来自 Stackoverflow API 的 GZIP-ed 响应?

转载 作者:可可西里 更新时间:2023-11-01 12:27:57 26 4
gpt4 key购买 nike

如何在 PHP 中读取来自 Stackoverflow API 的响应?响应是 GZIP 压缩的。我发现例如以下建议:

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

但是函数 http_inflate() 在我使用的安装中不可用。

还有其他一些简单的方法可以实现吗?

最佳答案

一个很酷的方式 http://www.php.net/manual/en/wrappers.compression.php

注意流包装器的使用,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"))));

或使用 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);

编辑--其他方法已删除,因为它们不发送 Accept-Encoding http header 。

关于php - 如何在 PHP 中读取来自 Stackoverflow API 的 GZIP-ed 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8581924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com