[英]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. 我的Facebook应用程序中有一个脚本,该脚本将在下拉列表中为用户提供他的Facebook朋友列表。 With the code below, I am able to grab the user's friends, but I am only getting the User ID. 使用下面的代码,我可以抓住用户的朋友,但是我只获得用户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
? 我该如何抓取它并仅回显$url
的名称?
{
"id": "4",
"name": "Mark Zuckerberg",
"first_name": "Mark",
"last_name": "Zuckerberg",
"link": "http://www.facebook.com/zuck",
"gender": "male",
"locale": "en_US"
}
看看JSON_decode http://www.php.net/manual/zh/function.json-decode.php
$data = json_decode($str);
echo $data['name'];
simple as that 就那么简单
From the PHP manual : 从PHP手册 :
$var = json_decode ( $result );
and echo what you want, like: echo $var ['link'];
并回显您想要的内容,例如: echo $var ['link'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.