我有两个网站和两个Facebook应用程序。现在我正在第二个网站上工作,我尝试在用Facebook登录后获取用户电子邮件地址。但是在另一个网站上工作的是什么,并没有解决这个问题。 这是我的代码:
session_start();
// added in v4.0.0
require_once 'autoload.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( '****************','******************************' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://mywebsite.com/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?locale=en_US&fields=id,name,email,first_name,last_name,gender,locale' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$fbemail = $graphObject->getProperty('email'); // To Get Facebook email ID
$fbfirst = $graphObject->getProperty('first_name');
$fblast = $graphObject->getProperty('last_name');
$fbgender = $graphObject->getProperty('gender');
$fblocale = $graphObject->getProperty('locale');
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $fbemail;
$_SESSION['FIRST'] = $fbfirst;
$_SESSION['LAST'] = $fblast;
$_SESSION['GENDER'] = $fbgender;
$_SESSION['LOCALE'] = $fblocale;
/* ---- header location after session ----*/
header("Location: index.php?uid=log-in");
} else {
$loginUrl = $helper->getLoginUrl( array('scope' => 'email'));
header("Location: ".$loginUrl);
}
我甚至没有在以下行中收到电子邮件地址:
$fbemail = $graphObject->getProperty('email'); // To Get Facebook email ID
感谢您的帮助或想法。
答案 0 :(得分:0)
$响应 - > getGraphObject(); 至 $响应 - > getGraphUser();