数组中的Httpful JSON

时间:2017-08-14 09:05:58

标签: php arrays json httpful

我已经尝试了3天让PHP使用PHP和httpful来显示来自此测试JSON的ID。

任何人都有任何想法,因为我尝试了大量不同的组合,甚至尝试创建一个处理程序来解码为数组...我只是吮吸PHP?

// Make a request to the GitHub API with a custom
// header of "X-Trvial-Header: Just as a demo".
<?php
include('\httpful.phar');

$url = "https://jsonplaceholder.typicode.com/posts";
$response = Httpful\Request::get($url)
    ->expectsJson()
    ->send();
echo "{$response[0]['id']}";

?>

My output... still nothing

1 个答案:

答案 0 :(得分:0)

您没有正确索引返回值。

响应是一个混合值。所以你应该做类似下面的事情来获得你想要的东西。

<?php
include('\httpful.phar');

$url = "https://jsonplaceholder.typicode.com/posts";
$response = Httpful\Request::get($url)
    ->expectsJson()
    ->send();

echo $response->body[0]->id;

?>