Hi Stackers,
我遇到了一个小问题,而且我在Stackoverflow上找不到有关此问题的工作解决方案。这完全与Facebook API有关。我在网站上创建了一个使用Facebook登录功能,这很好用。但是,我需要获取用户的电子邮件地址和性别。这是在basic_profile权限中,但我无法找到如何请求它,以便我可以将它用作变量。
FB-callback.php
/* INCLUSION OF LIBRARY FILEs*/
require_once( 'facebook/FacebookSession.php');
require_once( 'facebook/FacebookRequest.php' );
require_once( 'facebook/FacebookResponse.php' );
require_once( 'facebook/FacebookSDKException.php' );
require_once( 'facebook/FacebookRequestException.php' );
require_once( 'facebook/FacebookRedirectLoginHelper.php');
require_once( 'facebook/FacebookAuthorizationException.php' );
require_once( 'facebook/GraphObject.php' );
require_once( 'facebook/GraphUser.php' );
require_once( 'facebook/GraphSessionInfo.php' );
require_once( 'facebook/Entities/AccessToken.php');
require_once( 'facebook/HttpClients/FacebookCurl.php' );
require_once( 'facebook/HttpClients/FacebookHttpable.php');
require_once( 'facebook/HttpClients/FacebookCurlHttpClient.php');
/* USE NAMESPACES */
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\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;
session_start();
// Facebook info
//2.Use app id,secret and redirect url
$app_id = '';
$app_secret = '';
$redirect_url='';
//3.Initialize application, create helper object and get fb sess
FacebookSession::setDefaultApplication($app_id,$app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
$sess = $helper->getSessionFromRedirect();
//4. if fb sess exists echo name
if(isset($sess)) {
//create request object,execute and capture response
$request = new FacebookRequest($sess, 'GET', '/me');
// from response get graph object
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::className());
// use graph object methods to get user details
$name= $graph->getName();
$_SESSION["fb_access_token"] = $sess;
echo '<meta http-equiv="refresh" content="0;url=/account.php">';
exit("Please wait... If you do not get redirected <a href='/account.php'>click here</a>.");
}else{
$loginUrl = $helper->getLoginUrl();
//header("Location: " . $loginUrl);
echo '<meta http-equiv="refresh" content="0;url='. $loginUrl .'">';
exit("Please wait... If you do not get redirected <a href='". $loginUrl ."'>click here</a>.");
}
我需要的是获取此用户的电子邮件和性别的方法,以便我可以将其放在两个变量$email
和$gender
有人有任何想法吗?我忽视了什么吗?