Haaggis Alli Love
Haaggis Alli Love

Reputation: 19

Httpful JSON inside an array

I've been trying for 3 days to get PHP to show the ID from this test JSON using PHP and httpful.

Anyone have any ideas as i've tried loads of different combinations and even tried created a handler to decode as an array... I just suck at 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

Upvotes: 0

Views: 326

Answers (1)

Onwuka Gideon
Onwuka Gideon

Reputation: 411

You are not properly indexing the return value.

The response is a mixed value. So you should do something like below to get what you are looking for.

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

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

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

?>

Upvotes: 0

Related Questions