Shiki

I'm so bad with words that I can't even make a cool tagline

Getting a list of a Facebook user’s friends with their email addresses

This article assumes that you are familiar with the Facebook SDK Core Concepts and know how to request a user’s access token. This also uses the Facebook PHP SDK.

Getting the list of friends

You can get the friends of a Facebook user using this FQL query:

SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = $userId)

This request requires the access token of the user whose friends you want to access. Here’s an example using the Facebook SDK for PHP:

$client = new Facebook(array('appId'  => 'YOUR_APP_ID', 'secret' => 'YOUR_APP_SECRET'));

$userId = 123;
$fql = "SELECT uid, first_name, last_name FROM user "
     . "WHERE uid in (SELECT uid2 FROM friend where uid1 = $userId)";
$friends = $client->api(array(
  'method'       => 'fql.query',
  'access_token' => 'USER_ACCESS_TOKEN',
  'query'        => $fql,
));

// we now have an array containing the friends of the user
print_r($friends); 

You can look at the user table reference for other fields besides first_name and last_name. Note that there are some fields such as email that require user specific permissions. If the user/friend has not given your app email permissions, chances are you won’t be able to get the email address. For instances like these, you can opt to filter just the friends who have given your app permissions. This can be done using the is_app_user field:

SELECT uid, first_name, last_name FROM user 
  WHERE is_app_user = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $userId)

The case with email addresses

From my tests, it looks like it’s more reliable to get the email addresses of friends if you don’t put in any user access token when calling $client->api(). Here’s a complete example of getting a list of friends and getting all their email addresses:

$appId     = 'YOUR_APP_ID';
$appSecret = 'YOUR_APP_SECRET';

$userId          = 'A_FACEBOOK_USER_ID';
$userAccessToken = 'THE_FACEBOOK_USER_ACCESS_TOKEN';

$client = new Facebook(array('appId' => $appId, 'secret' => $appSecret));

// get all friends who has given our app permissions to access their data
$fql = "SELECT uid, first_name, last_name, email FROM user "
     . "WHERE is_app_user = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $userId)";
$friends = $client->api(array(
  'method'       => 'fql.query',
  'access_token' => $userAccessToken,
  'query'        => $fql,
));

// make an array of all friend ids
$friendIds = array();
foreach ($friends as $friend) {
  $friendIds[] = $friend['uid'];
}

// get info of all the friends without using any access token
$friendIds = implode(',', $friendIds);
$fql = "SELECT uid, first_name, last_name, email FROM user WHERE uid IN ($friendIds)";
$friends = $client->api(array(
  'method' => 'fql.query',
  'query'  => $fql,
  // don't use an access token
));

// we should now have a list of friend infos with their email addresses
print_r($friends);

24 Responses to Getting a list of a Facebook user’s friends with their email addresses

Hii, i have two lists of 100 nd 5 million facebook users. i will provide you if u are ready to pay $10(500 rupees). in advance if u want i will also give you some extra. so if u want to increase your facebook friends, just go to invite friends column ant copy paste these day by day..because these are huge…if any one intrested mail me on …rahulpandey_2009@hotmail.com asap.

Hey there,

Sorry to burst your bubble, but it is simply not possible to get the email addresses of Facebook users other than the user of your app (ie. the person who authorizes your app and for whom you get an access_token), not even Friends of the user. Even extended permissions won’t help here. I also tested it without an access_token, and that doesn’t work either.

@Dan. That is true, you won’t be able to get emails from users who have not authorized your app. You also won’t be able to get extended permissions if they haven’t authorized your app first. Sorry if I didn’t state that clearly.

Shiki,

The email returns null even though there are users in my friends list that have authorized the app. Can you please enlighten?

Hi! Can fql be used to get game scores. I have user permission for user_friendslist and user_game_activity. One by one I am able to get the scores “/me/scores” and check the array for game id and score. Since some people have around 1000 friends. Looping for 1000 requests takes time plus facebook may ban me for continous requests.

I was wondering if you know a smarter way to achieve it.

Hi Everyone,

I found that even if my friend is authorized to my app and given me also check ‘Allow friends to include my email address in Download Your Information’. But either way is not working.

Can anybody help me to fetch user’s email in any way.. I have access token , can set any permission and also set/ reset fb level settings.

Thanks in advance.

Hello ! It work but not returning email of friend. Please elaborate in details about permission to get friend’s email address.

Thanks.

Please help, i am not getting email of friends. i am getting id,first name and last name only.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>