I have to do a website with PHP and actually I'm trying with it. For now I want to obtain a JSON from a URL (I've got a web service with Node.js) and show in the screen. The URL returns a JSON object like this:
[{"name":"Juan","text":"Oh my god"},{"name":"Pedro","text":"I'm here"}]
I have this code in PHP file:
<?php
$data = file_get_contents('http://localhost:3000/node/busca'); // Returns the JSON
$terminos = json_decode($data);
print_r($terminos);
echo $terminos->name;
?>
But print_r returns:
Array (
[0] => stdClass Object (
[name] => Juan
[text] => Oh my god
)
[1] => stdClass Object (
[name] => Pedro
[text] => I'm here
)
)
The echo says
Notice: Trying to get property of non-object in C:...\index.php on line 17
What can I do? json_decode should return an object and not an array.
[…]array at the outset. → see