How to get email from facebook

匆匆过客 提交于 2019-12-06 16:29:41

use facebook->getLoginUrl with Email Permissions, see https://developers.facebook.com/docs/reference/login/email-permissions/ (scope' => 'email')

facebook.php:

<?
error_reporting(E_ALL);
ini_set('display_errors','on');
require 'facebook-php-sdk-master/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => <YOUR AppId>,
  'secret' => <YOUR APPsecret>,
));

$applicationurl = 'http://testdrive.nl/facebook.php';

// Get User ID
$user = $facebook->getUser();

if(empty($user))
{
    $params = array(
  'scope' => 'email',
  'redirect_uri' => $applicationurl
   );   

$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl ."\r\n");
exit;
}   

$fql = 'SELECT contact_email FROM user WHERE uid = '.$user;
$res = $facebook->api(array('method' => 'fql.query',
                            'query' => $fql));
//var_dump($res);
echo $res[0]['contact_email'];

You'll need to require an additional permission from your users in order to get their email address. This permission is aptly named email.

Depending on your method of authentication and user login, you'll have to add this email permission to your scope parameter. This scope parameter is where you add additional permissions to request from your user. You can ask for these permissions as the user installs your application but you can also request permission after the user has installed your application.

For more info on the email permission, check out this link to the relevant documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!