how to grab the json returned value in php?
I have a script in my facebook application which will provide the user with the list of his Facebook friends in a dropdown list. With the code below, I am able to grab the use开发者_运维百科r's friends, but I am only getting the User ID.
$api_key = 'xxx';
$secret = 'xxx';
include_once 'facebook.php';
$facebook = new Facebook($api_key, $secret);
$friends = $facebook->api_client->friends_get();
foreach ($friends as $friend)
{
echo $friend;// returns only the friend's ID and not name but i want to show the name
$url="https://graph.facebook.com/".$friend."/";
$res=file_get_contents($url);
}
With this link I want to grab the user's name and display it on a dropdown list. How can I grab it and echo only the name from $url
?
{
"id": "4",
"name": "Mark Zuckerberg",
"first_name": "Mark",
"last_name": "Zuckerberg",
"link": "http://www.facebook.com/zuck",
"gender": "male",
"locale": "en_US"
}
Take a look at JSON_decode http://www.php.net/manual/en/function.json-decode.php
From the PHP manual:
$var = json_decode ( $result );
and echo what you want, like: echo $var ['link'];
$data = json_decode($str);
echo $data['name'];
simple as that
精彩评论