如何获取用户的Facebook好友的姓名?
我发送请求使用FB.ui({ method: 'apprequests'?
这是我的代码:
<input id="btSelectFriends" type="button" value="Select Friends" onclick="javascript:FBUISelectFriends()" />
<div id="fb-root"></div>
<script type="text/javascript" src="https://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
function FBUISelectFriends()
{
var selectedFriendsRequest_ids;
FB.ui({ method: 'apprequests', message: 'Select friends', data: 'tracking information for the user', tite: 'Select friends' },
function (response) {
if (!response || !response.request_ids) { alert('Request could not be sent'); return; }
selectedFriendsRequest_ids = response.request_ids;
for (var i = 0; i < selectedFriendsRequest_ids.length; i++) {
FB.api('/me/apprequests/?request_ids=' + selectedFriendsRequest_ids[i].toString(),
function (response) {
if (!response || response.error) { alert('Friends Selection error occured'); return; }
}
)
FB.api(selectedFriendsRequest_ids[0],
function (response)
{
alert(response.name);
}
);
}
}
);
return;
}
</script>
我尝试了这段代码但是没有用:
FB.api(selectedFriendsRequest_ids[0], function (response) {console.log(response.name);});
你能帮忙吗?
感谢。
答案 0 :(得分:1)
只需在功能中更改此内容
功能FBUISelectFriends() {
FB.ui(
{
method: 'apprequests',
redirect_uri: 'YOUR APP URL',
message: "Tom sent you a request"
},
function(response) {
if(response && response.hasOwnProperty('to')) {
for(i = 0; i < response.to.length; i++) {
//alert( response.to[i]);
// response.to[i] gives the selected facebook friends ID
// To get name of selected friends call this function below
getfbnames(response.to[i]);
}
}
}
);
}
function getfbnames(selectedfrndid) {
var url = 'getfriendfbname.php'; // call a php file through URL and jquery ajax
$.ajax({
type:'POST',
url: url,
data : { fbid : selectedfrndid },
async: false,
success: function(data)
{
alert(data);
}
}); // end of ajax
}
一个文件getfriendfbname.php,它使用php中的朋友facebook id返回facebook朋友姓名的名称
$fbid=$_POST['fbid'];
$json = file_get_contents('https://graph.facebook.com/'.$fbid);
$data = json_decode($json);
echo $data->name;
return $data->name;
答案 1 :(得分:0)
自9月30日以来,Facebook发布了他们系统的 Requests 2.0 实施,这比旧更有用。 原因如下:
您只需在应用信息中心中激活请求2.0 ,然后只需更改Javascript回调中的条件即可。
要求检索有关您用户的朋友FB.api('/me/friends?fields=id,name,first_name,last_name
的信息,您只需要使用您发送请求的Facebook用户ID解析此请求的结果!
这里解释了所有内容关于请求2.0 :http://developers.facebook.com/blog/post/569/