2011-01-31 19 views
1

我在我的Facebook应用程序中有一个脚本,它将在下拉列表中向用户提供他的Facebook朋友列表。使用下面的代码,我可以获取用户的朋友,但我只能获取用户ID。如何在php中抓取json返回的值?

$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); 
} 

随着this link我想抓住用户的姓名和下拉列表上显示它。我怎样才能抓住它,只回声$url的名字?

{ 
    "id": "4", 
    "name": "Mark Zuckerberg", 
    "first_name": "Mark", 
    "last_name": "Zuckerberg", 
    "link": "http://www.facebook.com/zuck", 
    "gender": "male", 
    "locale": "en_US" 
} 

回答

2

From the PHP manual

$var = json_decode ($result); 

和回声你想要的东西,如:echo $var ['link'];

+0

错误的答案。应该使用json_decode而不是json_encode。 – Gajus 2011-01-31 17:42:59

2
$data = json_decode($str); 
echo $data['name']; 

这么简单