我收到了一个 API url feed,它以 JSON 格式返回响应,并被告知将 header 设置为:
Accept: application/json
X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009
有人可以告诉我如何解决这个问题吗?
最佳答案
问题 1
我会使用PHP curl图书馆。
例如:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009',
));
// grab URL and pass it to the browser
echo curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
参见curl_setopt()
有关常量的更多信息,例如我上面使用的 CURLOPT_HTTPHEADER
。
评论中的问题 2
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'X-some-API-Key: fdfdfdfdsgddc43aa96c556eb457b4009',
));
// grab URL and pass it to the browser
$json = json_decode(curl_exec($ch), true);
// close cURL resource, and free up system resources
curl_close($ch);
$json
现在包含响应的关联数组,您可以var_dump()
查看其结构。
关于php - 如何从以 json 格式发送响应的 URL 读取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5978090/