如何使用FB.ui获取我发送请求的朋友的姓名({method:'apprequests'

时间:2011-10-16 22:09:08

标签: javascript facebook sdk friend

如何获取用户的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);});

你能帮忙吗?

感谢。

2 个答案:

答案 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 实施,这比更有用。 原因如下:

  • 使用旧版,您的用户首先发送了请求,Facebook将 request_ids 返回给您,然后您必须验证您向谁发送了请求... < / LI>
  • 使用新的,针对每组请求(例如,当用户使用多朋友选择器发送多个请求时),Facebook会返回一个请求ID(参数) : response.request )并将所有Facebook用户ID请求的参数发送到(参数: response.to

您只需在应用信息中心中激活请求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/