PHP facebook登录无法从用户那里收到电子邮件

时间:2014-12-31 20:28:24

标签: php facebook email

我使用代码通过Facebook登录并获取包含电子邮件在内的用户信息..它大部分都有效,但我无法在我的阵列中收到电子邮件...

这是我的代码:

<?php
session_start(); //Session should always be active

$app_id             = 'appID';  //localhost
$app_secret         = 'secret';
$required_scope     = 'public_profile, publish_actions, email'; //Permissions required
$redirect_url       = 'URL'; //FB redirects to this page with a code

require_once __DIR__ . "/facebook-php-sdk-v4-4.0-dev/autoload.php"; //include autoload from SDK folder

//import required class to the current scope
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;

FacebookSession::setDefaultApplication($app_id , $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);

try {
  $session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
    die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
    die(" Error : " . $ex->getMessage());
}



if ($session){      

    $user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());


    //save session var as array
    $_SESSION["fb_user_details"] = $user_profile->asArray(); 

    $user_id = ( isset( $_SESSION["fb_user_details"]["id"] ) )? $_SESSION["fb_user_details"]["id"] : "";
    $user_name = ( isset( $_SESSION["fb_user_details"]["name"] ) )? $_SESSION["fb_user_details"]["name"] : "";
    $user_email = ( isset( $_SESSION["fb_user_details"]["email"] ) )? $_SESSION["fb_user_details"]["email"] : "";

if(isset($_SESSION["fb_user_details"]))
    {
        print 'Hi '.$_SESSION["fb_user_details"]["name"].' you are logged in! [ <a href="?log-out=1">log-out</a> ] ';
        print '<pre>';
        print_r($_SESSION["fb_user_details"]);
        print '</pre>';
    }



}else{ 

    echo "we dont have a session";

    //session var is still there
    if(isset($_SESSION["fb_user_details"]))
    {
        print 'Hi '.$_SESSION["fb_user_details"]["name"].' you are logged in! [ <a href="?log-out=1">log-out</a> ] ';
        print '<pre>';
        print_r($_SESSION["fb_user_details"]);
        print '</pre>';
    }
    else
    {
        //display login url 
        $login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
        echo '<a href="'.$login_url.'">Login with Facebook</a>'; 
    }
}

我的结果是:

Array
(
[id] => 
[first_name] => 
[gender] =>
[last_name] => 
[link] => 
[locale] => 
[name] => 
[timezone] => 
[updated_time] => 
[verified] => 
)

我的阵列中没有电子邮件?有什么想法吗?

我也试过添加这个

$user_profile->getProperty("email");

这一行之后

$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());

仍然注意到......我的许可似乎没问题......

1 个答案:

答案 0 :(得分:0)

正如文档所说(https://developers.facebook.com/docs/graph-api/reference/v2.2/user?locale=en_GB):

此人在其个人资料中列出的主要电子邮件地址。 如果没有有效的电子邮件地址,则不会返回此字段。

因此,它不会返回您正在登录的电子邮件,而是返回您个人资料中列出的电子邮件。