How to get email from facebook

匆匆过客 提交于 2020-01-02 22:59:06

问题


I have created an application. There is form, in which there are two fields email and name. There is button, which enable you to login with facebook and after that it will get the user name and email.

I have tried many ways. I can get name and other detail, but I couldn't get user email. I have also used fql to get the email but didn't get. Below is the code. I want to retrive that by PHP.

$fql = 'SELECT contact_email FROM user WHERE uid = '.$user;
$res = $facebook->api(array('method' => 'fql.query',
                            'query' => $fql));

Can any one will tell me how to get the email. Any help will be appreciable.


回答1:


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'];



回答2:


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.



来源:https://stackoverflow.com/questions/16262252/how-to-get-email-from-facebook

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