Reputation: 8995
<?php
$handle = fopen("https://graph.facebook.com/[email protected]&type=user&access_token=2227472222|2.mLWDqcUsekDYK_FQQXYnHw__.3600.1279803900-100001310000000|YxS1eGhjx2rpNYzzzzzzzLrfb5hMc.", "rb");
$json = stream_get_contents($handle);
fclose($handle);
echo $json;
$obj = json_decode($json);
print $obj->{'id'};
?>
Here is the JSON: {"data":[{"name":"Sinan \u00d6zcan","id":"610914868"}]}
It echos the JSON but I was unable to print the id.
Also I tried:
<?php
$obj = json_decode($json);
$obj = $obj->{'data'};
print $obj->{'id'};
?>
Upvotes: 4
Views: 3902
Reputation: 6992
It's a bit more complicated than that, when you get an associative array out of it:
$json = json_decode('{"data":[{"name":"Sinan \u00d6zcan","id":"610914868"}]}', true);
Then you may echo the id with:
var_dump($json['data'][0]['id']);
Without assoc, it has to be:
var_dump($json->data[0]->id);
Upvotes: 1
Reputation: 523684
Note that there is an array in the JSON.
{
"data": [ // <--
{
"name": "Sinan \u00d6zcan",
"id": "610914868"
}
] // <--
}
You could try $obj = $obj->{'data'}[0]
to get the first element in that array.
Upvotes: 4
Reputation: 21962
Have you tried $obj->data
or $obj->id
?
Update: Others have noted that it should be $obj->data[0]->id
and so on.
PS You may not want to include your private Facebook access tokens on a public website like SO...
Upvotes: 2
Reputation: 57845
It looks like the key "data"
is an array of objects, so this should work:
$obj = json_decode($json);
echo $obj->data[0]->name;
Upvotes: 3