我想在用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()
功能,可以在數據庫中存儲的電子郵件地址。我會很感激這個幫助。
感謝您的回覆,我將測試它讓你知道@Tobi –
謝謝,我可以得到其他數據用逗號分隔嗎?如'範圍'=>'電子郵件','生日','國家' –
你不應該猜字段名稱,但看看https://developers.facebook.com/docs/graph-api/reference/上的文檔用戶#閱讀 – Tobi