如何使用 FB.ui({ method: 'apprequests'

发布于 12-10 09:28 字数 1648 浏览 0 评论 0原文

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

你能帮忙吗?

谢谢。

How can I get the names of the Facebook friends of the user?
I send requests to using FB.ui({ method: 'apprequests'?
Here is my code:

<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>

I tried this code but it didn't work:

FB.api(selectedFriendsRequest_ids[0], function (response) {console.log(response.name);});

Can you please help?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笛声青案梦长安2024-12-17 09:28:14

只需在您的函数

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;

Just change this in your function

function 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
}

A file getfriendfbname.php which returns the name of facebook friend name using friend facebook id in php

$fbid=$_POST['fbid'];
$json = file_get_contents('https://graph.facebook.com/'.$fbid);
$data = json_decode($json);

echo $data->name;
return $data->name;
吻风2024-12-17 09:28:14

自 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 用户解析此请求的结果您向其发送请求的 ids !

这里关于请求 2.0 进行了解释:http://developers.facebook.com/blog/post/569/

Since 30 Sept. Facebook released a Requests 2.0 implementation of their system, which is a LOT more useful than the old one.
Here's why :

  • With the old one, your user first sent requests, Facebook returned to you the request_ids, then you had to verify who you sent the requests to...
  • With the new one, for each group of requests (like when a user sends multiple requests using multi-friend-selector) Facebook returns you one request id (parameter : response.request) and a parameter with all Facebook user ids requests were sent to (parameter : response.to)

You just have to activate Requests 2.0 in your app dashboard, then just change the conditions in your Javascript callback.

There's requests to retrieve information about your user's friends FB.api('/me/friends?fields=id,name,first_name,last_name and you just have to parse the result of this request with the Facebook user ids you sent requests to !

Everything is explained here about Requests 2.0 : http://developers.facebook.com/blog/post/569/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文