Alaeddin Mohd
Alaeddin Mohd

Reputation: 1

How can I get the names of the friends I send requests to using FB.ui({ method: 'apprequests'

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.

Upvotes: 0

Views: 2015

Answers (2)

Tousif Jamadar
Tousif Jamadar

Reputation: 141

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;

Upvotes: 1

evanesis
evanesis

Reputation: 258

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/

Upvotes: 0

Related Questions