1、创建应用 https://developers.facebook.com/
image.png
2、进入应用,添加产品:Facebook登录
点击设置-》基本 获取编号秘钥
进入Facebook登录-》设置 来设置允许的回调地址
3、下载SDK:https://github.com/facebookarchive/php-graph-sdk
4、获取登录链接并跳转
require APP_ROOT_PATH."system/Facebook/autoload.php";
//创建实例
$fb = new \Facebook\Facebook([
'app_id' => '你的应用编号', // 这里就是应用的APPID
'app_secret' => '你的秘钥', //这里填写应用的密钥
'default_graph_version' => 'v2.10',//这里sdk的版本号
]);
$helper = $fb->getRedirectLoginHelper();
//需要获取的登录用户参数,有的参数需要权限
$permissions = ['email']; // Optional permissions\
$aUrl = 你的回调地址;
//获取到的登录连接
$loginUrl = $helper->getLoginUrl($aUrl, $permissions);
// $loginUrl = htmlspecialchars($loginUrl);
return redirect($loginUrl);
5、回调
session_start();
require APP_ROOT_PATH."system/Facebook/autoload.php";
$fb = new \Facebook\Facebook([
'app_id' => '你的编号', // 这里就是应用的APPID
'app_secret' => '你的秘钥', //这里填写应用的密钥
'default_graph_version' => 'v2.10',//这里sdk的版本号
]);
//var_dump($_GET['state']);
$helper = $fb->getRedirectLoginHelper();
if(isset($_GET['state'])){
$_SESSION['FBRLH_state']=$_GET['state'];
}
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
$lUrl = get_domain().url("index","user#login");
app_redirect($lUrl);
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
// echo '<h3>Metadata</h3>';
// var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('你的编号');
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
}
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
// Returns a `Facebook\FacebookResponse` object
//fields后面的参数是要获取的用户信息
$response = $fb->get('/me?fields=id,name', $_SESSION['fb_access_token']);//<strong>$_SESSION['fb_access_token']</strong>
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
//获取用户Facebook信息 id:关联Facebookid;name:姓名
$user = $response->getGraphUser();
//echo 'Name: ' . $user['id'];
//拿到这些内容之后,再根据自己的逻辑进行处理
网友评论