2015-05-08 71 views
1

我想在用facebook登錄我的網站時捕獲用戶的電子郵件地址。我有以下代碼:無法弄清楚如何從facebook獲取電子郵件PHP SDK

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()功能,可以在數據庫中存儲的電子郵件地址。我會很感激這個幫助。

回答

0

它屬於你的最後else塊:

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

感謝您的回覆,我將測試它讓你知道@Tobi –

+0

謝謝,我可以得到其他數據用逗號分隔嗎?如'範圍'=>'電子郵件','生日','國家' –

+1

你不應該猜字段名稱,但看看https://developers.facebook.com/docs/graph-api/reference/上的文檔用戶#閱讀 – Tobi

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

使用此參數的電子郵件ID 區域= EN_US &欄=姓名,電子郵件

$請求=新FacebookRequest? ($ session,'GET','/ me?locale = en_US & fields = name,email');

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