【问题标题】:How can I get the names of the friends I send requests to using FB.ui({ method: 'apprequests'如何使用 FB.ui({ method: 'apprequests'
【发布时间】:2011-10-16 22:09:07
【问题描述】:

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

你能帮忙吗?

谢谢。

【问题讨论】:

    标签: javascript facebook sdk friend


    【解决方案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]);
                    }               
                }
           }
         );
    

    }

    函数 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;
    

    【讨论】:

      【解决方案2】:

      自 9 月 30 日以来,Facebook 发布了他们系统的 Requests 2.0 实现,它比 更有用。 原因如下:

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

      您只需在应用仪表板中激活 Requests 2.0,然后只需更改 Javascript 回调中的条件即可。

      有请求检索有关您用户的朋友FB.api('/me/friends?fields=id,name,first_name,last_name 的信息,您只需使用您向其发送请求的 Facebook 用户 ID 解析此请求的结果!

      这里解释了 关于 Requests 2.0 的所有内容:http://developers.facebook.com/blog/post/569/

      【讨论】:

      • 如何在您的应用仪表板中激活 Requests 2.0?
      • 看看我帖子末尾链接中的性能改进部分:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      相关资源
      最近更新 更多