How to get userids of Facebook friends from Friend Selector dialog box


Recently I was developing a Facebook app for a client which requires to invite user friends. For this I used the FB.ui apprequests method. I also need to get the user ids of Facebook friends the user has invited. My initial approach was to print the response of the apprequests method, but it gave undefined object error. Here is what I tried initially and that didn’t return the userids.

FB.ui({ method: 'apprequests',message: 'Some message'}, function(res){
echo res; //echo res.error;  echo res.description;
});

But it was quite clear that this is not the right way. After some searching I got it right. I am sharing so that somebody else can use it.

FB.ui({ method: 'apprequests',message: 'Some message'}, function(res){
        	var req_ids = res.request_ids;
    		var _batch = [];
    		for(var i=0; i<req_ids.length; i++ ){
        		_batch.push({"method":"get","relative_url":req_ids[i]});
    		}
    		if(_batch.length>0){
        		FB.api('/','POST',{batch:_batch},function(res){
            		var IDs='';
        		for (var j = 0; j<res.length; j++) {
                       		body = res[j].body;
                                var myObject = eval('(' + body + ')');
                                friendID = myObject.to.id; 
                                echo friendID;  //one friend id
                              	}
             		});     
    		}
});

Hope now that you can get Facebook friends userids from the friend selector dialog box. In the future post I will be discussing how these userids can be used in our app to send Facebook Events invite to users friends.

Subscribe to My Newsletter
Receive all new posts and site updates in your mailbox.
Name:
Email:
 
Powered by Optin Form Adder

Tags: , , ,

1 Response to "How to get userids of Facebook friends from Friend Selector dialog box"

  • ers says:
Leave a Comment