开发者

How to get photo and name from several ids at once

I would like to show to my visitor their friends in an app. I get the开发者_如何学运维 id's of the friends in the app, but how can I get the names and pictures of them at once? Is it possible?


Once the user has approved your app, you could this FQL query:

SELECT uid, name, pic FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
AND is_app_user = 1

Full function:

function getFriends() {  
    FB.login(function(response) {
      if (response.session && response.perms) {
        FB.api(
          {
            method: 'fql.query',
            query: 'SELECT uid, name, pic FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1'
          },
          function(response) {
            alert('Friends using app: ' + JSON.stringify(response));
          }
        );
      }
    } , {perms:''}); 
}


Use the fields parameter in the API request:

fields=name,id,picture

By default the Graph API only returns name and id. Specifying that you want their profile picture in the fields list will also give you the picture. So the full request using the Javascript SDK would look something like this:

FB.api('/me/friends', { fields: 'name,id,picture' }, function(response) {
  // ...
}

UPDATE: to get pictures for a set of IDs you already have, you have to use FQL like so:

SELECT uid, name, pic FROM user
  WHERE uid IN (uid1, uid2, uid3,...) 

Replacing uid1 etc. with your list of IDs.

Alternatively, you could use a batch request to issue several API requests to /uidX/picture at once. I don't know how much the javascript SDK will help you with that though; it may require more work than it's worth. I'd just go with the FQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜