chell
chell

Reputation: 7866

How to get invited users ID from response object FB.ui

I am using the new FB.ui to create an request to friends on Facebook for my application.

The user is show a panel with their friends and they select the ones they want to send the request.

On the callback I can access the request_id and now I want to get the details for the users that were invited.

If I paste the following code in the browser I can get the user_id of the invited user which is the info I want:

https://graph.facebook.com/138744992885393?access_token=193078857407882|d0048a9beb58a9a247c6b987.0-751640040|zNmBLfZxBBKikoj8RlZiHfKpugM

What I want to be able to do is do the same thing but from my code and then access the information returned.

Here is my code:

    function sendRequests() {
        FB.ui({
            method: 'apprequests',
            message: ' should learn more about this awesome site.',
            data: 'extra data'
        }, function(response) {
            if (response != null && response.request_ids && response.request_ids.length > 0) {
                for (var i = 0; i < response.request_ids.length; i++) {
                    alert("Invited: " + response.request_ids[i]);
// somehow send a request to Facebook api to get the invited user id from the request_id   and save to the database                 
//can save these id's in the database to be used to track the user to the correct page in application.

                }
             top.location.href="http://localhost:3000/";
            } else {
                alert('No invitations sent');
            }
        });
    }

How can I do this?

I am using Rails 3.0.7 Ruby 1.9.2

Upvotes: 1

Views: 1799

Answers (3)

Tousif Jamadar
Tousif Jamadar

Reputation: 141

You can also get the facebook IDs and even names of selected friends using below code:

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

chell
chell

Reputation: 7866

You can get the facebook user ID as follows:

for (var i = 0; i < req_ids.length; i++) {

        alert("Invited: " + req_ids[i]);
        FB.api('/me/apprequests/?request_ids='+toString(req_ids[i]),
        function(response)
        { alert(response);
            alert(response['data'][0]['from']['id']);
            });

                               }

Thanks very much to this post on stackoverflow

Upvotes: 1

pixelastic
pixelastic

Reputation: 5278

If that's the user_id of the invited users, you can have them in your callback, in response.request_ids. But you seem to already know that.

Could you clarify your problem?

Upvotes: 0

Related Questions