How can I get facebook friends list email addresses?

前端 未结 6 885
春和景丽
春和景丽 2020-12-06 06:31

Is there anyone who knows how to get a friends list of email address using Facebook PHP SDK?

6条回答
  •  粉色の甜心
    2020-12-06 07:06

    Get information from this links:

    Procedure

    Statckoverflow discussion

    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)
    

    Get Updated Answer:

    $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);
    

提交回复
热议问题