无法弄清楚如何从facebook PHP SDK获取电子邮件

时间:2015-05-08 06:50:29

标签: php facebook facebook-graph-api facebook-php-sdk

我想在用facebook登录我的网站时捕获用户的电子邮件地址。我有以下代码:

session_start();
added in v4.0.0
require_once 'autoload.php';
/******function to store users' value******/
require_once 'functions.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( '*****app id****','***Secret key****' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://mykadamtala.com/facebook/fbconfig.php' );


try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();

     // get response
        $graphObject = $response->getGraphObject();
        $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
        $firstname = $graphObject->getProperty('first_name');
        $lastname = $graphObject->getProperty('last_name');
        $fbfullname = $graphObject->getProperty('name');
        $gender = $graphObject->getProperty('gender');
        $femail = $graphObject->getProperty('email'); //This of course does not work


    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;  
        $_SESSION['EMAIL'] =  $femail; //Nothing stores here
        $_SESSION['GENDER'] = $gender;             
        $_SESSION['FULLNAME'] = $fbfullname;

        adduser($fbid,$firstname, $lastname, $femail);
    /* ---- header location after session ----*/
  header("Location: index.php");
} else {
  $loginUrl = $helper->getLoginUrl();
 header("Location: ".$loginUrl);
}

用户的ID,性别,名字,姓氏都存储得很好。但电子邮件未被捕获。在这里搜索我得到了这个插入脚本:

 $getemail = $helper->getLoginUrl(array(
   'scope' => 'email
 ));

但我很困惑在哪里包含它以及如何最终将电子邮件存储在变量中,以便我的adduser()函数可以将电子邮件地址存储在数据库中。我会非常感谢你的帮助。

3 个答案:

答案 0 :(得分:1)

$request = new FacebookRequest( $session, 'GET', '/me?  fields=id,name,first_name,last_name,gender,email' );
$response = $request->execute();

$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id');
$fbfullname = $graphObject->getProperty('name');full name
$femail = $graphObject->getProperty('email');
$firstname = $graphObject->getProperty('first_name');
$lastname = $graphObject->getProperty('last_name');
$gender = $graphObject->getProperty('gender');

答案 1 :(得分:1)

使用此代码

session_start();
added in v4.0.0
require_once 'autoload.php';
/******function to store users' value******/
require_once 'functions.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( '*****app id****','***Secret key****' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://mykadamtala.com/facebook/fbconfig.php' );


try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me?  fields=id,name,first_name,last_name,gender,email' );
  $response = $request->execute();

     // get response
        $graphObject = $response->getGraphObject();
        $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
        $firstname = $graphObject->getProperty('first_name');
        $lastname = $graphObject->getProperty('last_name');
        $fbfullname = $graphObject->getProperty('name');
        $gender = $graphObject->getProperty('gender');
        $femail = $graphObject->getProperty('email'); //This of course does not work


    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;  
        $_SESSION['EMAIL'] =  $femail; //Nothing stores here
        $_SESSION['GENDER'] = $gender;             
        $_SESSION['FULLNAME'] = $fbfullname;

        adduser($fbid,$firstname, $lastname, $femail);
    /* ---- header location after session ----*/
  header("Location: index.php");
} else {
  $loginUrl = $helper->getLoginUrl(array(
   'scope' => 'email'
  ));
  header("Location: ".$loginUrl);
}

它属于您的上一个else区块:

...
else {
  $loginUrl = $helper->getLoginUrl(array(
   'scope' => 'email'
  ));
  header("Location: ".$loginUrl);
}

答案 2 :(得分:0)

它属于您的上一个else区块:

...
else {
  $loginUrl = $helper->getLoginUrl(array(
   'scope' => 'email'
  ));
  header("Location: ".$loginUrl);
}