我使用的代码通过 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 FacebookFacebookSession;
use FacebookFacebookRequest;
use FacebookGraphUser;
use FacebookFacebookRedirectLoginHelper;

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

仍然注意...我的权限似乎是确定。.

2014-12-31 00:00:00
问题评论:

回答:

根据文档指出 (https://developers.facebook.com/docs/graph-api/reference/v2.2/user?locale=en_GB):

在他们的配置文件中列出的此人的主电子邮件地址。此字段不会返回任何有效的电子邮件地址是否可用。

因此不返回电子邮件您可以记录,但一个在您的配置文件中列出。

内容来源于Stack Overflow PHP facebook login can't get email from user
请输入您的翻译

PHP facebook login can't get email from user

确认取消