1、创建应用 https://developers.facebook.com/
2、进入应用,添加产品:Facebook登录
点击设置-》基本 获取编号秘钥
记录facebook登录的设置
/**
* app端授权,php登陆
* composer require facebook/graph-sdk
* @Author wzb
* @Date 2022/4/23 12:05
*/
public function facebook()
{
$access_token = $this->request->param('access_token', '', 'trim');
if (!$access_token) {
exit('服务器错误');
}
$fb = new Facebook([
'app_id' => '你的应用编号',
'app_secret' => '你的秘钥',
'default_graph_version' => 'v2.10',
]);
$facebook_user = [];
$avatar_url = '';
try {
$response = $fb->get(
'/me',
$access_token
);
$facebook_user = $response->getDecodedBody();
if (!isset($facebook_user['id'])) {
exit('获取Facebook信息失败');
}
//获取用户头像
$response = $fb->get(
"/{$facebook_user['id']}/picture?type=large&redirect=false",
$access_token
);
$facebook_user_avatar = $response->getDecodedBody();
if (isset($facebook_user_avatar['data']['url'])) {
$avatar_url = $facebook_user_avatar['data']['url']; // 获取头像
}
} catch (\Exception $e) {
exit('系统繁忙,请稍后再试');
}
$fbUid = $facebook_user['id'] ?? ''; // fb_uid
$nickname = $facebook_user['name']; // 呢称
//2.拿到用户信息进行用户登录or用户创建流程
}
1、进入Facebook登录-》设置 来设置允许的回调地址
2、下载SDK:https://github.com/facebookarchive/php-graph-sdk
/**
* facebook登陆
* 下载SDK:https://github.com/facebookarchive/php-graph-sdk
* @Author wzb
* @Date 2022/4/23 11:53
*/
function facebook_login(){
session_start();
$fb = new \Facebook\Facebook([
'app_id' => '你的应用编号',
'app_secret' => '你的秘钥',
'default_graph_version' => 'v3.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$redirect_uri = 'https://m.remixg.com/Login/face_login'; // 授权地址必须要填写跟这个一样
$loginUrl = $helper->getLoginUrl($redirect_uri, $permissions);
echo '. htmlspecialchars($loginUrl) . '">Log in with Facebook!';
}
/**
* facebook登陆回调
* @Author wzb
* @Date 2022/4/23 11:53
*/
function face_login(){
session_start();
$fb = new \Facebook\Facebook([
'app_id' => '你的应用编号',
'app_secret' => '你的秘钥',
'default_graph_version' => 'v3.2',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
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";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// 获取 accessToken
$oAuth2Client = $fb->getOAuth2Client();
// 获取 userid
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
// 获取用户信息
$userinfo = $fb->get($tokenMetadata->getUserId() . '?fields=id,name,first_name,picture', $accessToken->getValue());
$userinfo = json_decode($userinfo->getBody(), true);
// 执行注册
}
/**
* facebook js login
*/
function login_facebook(){
FB.login(function(response) {
if (response.status === 'connected') {
FB.api('/me', {fields: 'id,name,first_name,picture.width(120).height(120)'},function(res) {
var facebook_login_data = {id:res.id,name:res.name,picture:res.picture.data.url};
// 传给后台处理注册登录
AjaxJson(facebook_login_url,facebook_login_data,function (data) {
if(data.status == 1){
window.location.reload();
}else{
console.log('登陆失败');
}
})
});
} else {
console.log('该用户没有登录');
}
}, {scope: 'public_profile,email'});
}
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
window.fbAsyncInit = function() {
FB.init({
appId : '你的应用编号',
cookie : true, // 启用cookie
xfbml : true, // 解析此页面上的社交插件
version : 'v3.2' // 使用图形api v3.2版本
});
};