Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making multiple fb.api calls

I've been just starting on Facebook Graph API.

How can I call several attributes with just 1 FB.api call? Right now, I have

        FB.api('/me', function(me){
          if (me) {
            var myEmail = me.email;
            var myID = me.id;
            var myFirst_name = me.first_name;
            //Other attributes
          }
        });

and

        FB.api('/me/friends',{ fields: 'name,id' }, function(response){
            var friends = response.data;
        }
       });

How do I combine both API calls into one, like say, just 1 FB.api() call?

All responses will really really help.

like image 511
bryan.blackbee Avatar asked Jun 21 '12 08:06

bryan.blackbee


1 Answers

How do I combine both api calls into one, like say, just 1 FB.api() call?

You could combine them both into one Batch Request – but that’ll make dealing with the response a little more complex, and I’m not sure if it would bring any “performance” gain in such a simple use case anyway.

EDIT:

There is a new way now¹ using the field expansion feature:

/me?fields=id,email,first_name,friends.fields(id,name)

– this would get you the requested fields id, email and first_name for the current user, and also the id and name for their friends. (Actually, just ,friends would do if you only want their id and name, because those are the default fields the API delivers – but if you want other fields as well, you can add them using ,friends.fields(…).)


¹ actually for quite a while now, but this question has come to my attention again now, so I thought I’d add that.

like image 133
CBroe Avatar answered Sep 20 '22 19:09

CBroe