简体   繁体   中英

Facebook login Api : unable to get email in php

Here, I am trying to get user email from facebook login api. By default i am getting id and name but unable to get email. Any help would be appreciated.

I have done with following. Please look in to this

$facebook = new Facebook(array(
                    'appId'     => xxxxxxxxxxxxx,
                    'secret'    =>  xxxxxxxxxxxxxxxxxxxxxxxx,
                ));

$facebook->setAccessToken($facebook->getAccessToken());
$user = $facebook->getUser();

if($user){
$user_profile = $facebook->api('/me?locale=en_US&fields=first_name,last_name,email');
echo "<pre>"; print_r($user_profile); exit;
}

if(empty($user)){ 
    $loginurl = $facebook->getLoginUrl(array('scope'=> 'email, publish_actions, user_birthday, user_location, user_work_history, user_hometown, user_photos',
            'redirect_uri'  =>  $this->config->getConfig()->getBaseUrl().'social/index/facebooklogin',
            'display'=>'popup'
        ));
    $this->_redirect($loginurl);
}

Output :

Array
(
    [first_name] => xxxx
    [last_name] => xxxxxx
    [id] => xxxxxxxxx
)

I have gone through the some posts, posted on this portal. But not enough to solve my problem.

Thanks,

Your app may be missing the email permission. It is usually automatically assigned but to set it manually, go to your app panel and do this:

在此输入图像描述

In app review now you should see the email permission with a green light. Now you are good to go.

However, notice that you are not always able to get user's email. First of all, in order to get the email, the user has to be an email. Quote from FB documentation :

Note, even if you request the email permission it is not guaranteed you will get an email address. For example, if someone signed up for Facebook with a phone number instead of an email address, the email field may be empty.

Also, consider that in order to get an user email, that user has to authorize your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM