基于 overtrue/socialite改造的,适用于easyswoole的第三方登录组件,现已支持wechat,qq,weibo,github,facebook
PHP >= 7.0
swoole >=4.4.0
$ composer require "xbing2002/socialite" "1.0"
authorize.php
:
<?php
use Overtrue\Socialite\SocialiteManager;
$config = [
'wechat' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$socialite = new SocialiteManager($config);
$socialite->driver('wechat')->redirect();
callback.php
:
<?php
use Overtrue\Socialite\SocialiteManager;
$config = [
'wechat' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$socialite = new SocialiteManager($config);
$user = $socialite->driver('wechat')->user();
$user->getId(); // openid
$user->getNickname(); // "昵称"
$user->getName(); // "昵称"
$user->getAvatar(); // 头像
$user->getProviderName(); // WeChat
...
现在支持:
facebook
, github
, weibo
, qq
, wechat
.
每一个登录平台的配置都是一样的,只需要配置: client_id
, client_secret
, redirect
.
例子:
...
'weibo' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
...
有些登录平台可以在跳转之前设置Scope:
$response = $socialite->driver('github')
->scopes(['scope1', 'scope2'])->redirect();
WeChat scopes:
snsapi_base
,snsapi_userinfo
- 用于公众号登录.snsapi_login
- 用户web登录.
当然你也可以动态设置跳转链接:
$socialite->redirect($url);
// or
$socialite->withRedirectUrl($url)->redirect();
// or
$socialite->setRedirectUrl($url)->redirect();
如果存在一些自定义参数,请用with方法
$response = $socialite->driver('google')
->with(['hd' => 'example.com'])->redirect();
$user = $socialite->driver('weibo')->user();
{
"id": 1472352,
"nickname": "overtrue",
"name": "安正超",
"email": "[email protected]",
"avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
"original": {
"login": "overtrue",
"id": 1472352,
"avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/overtrue",
"html_url": "https://github.com/overtrue",
...
},
"token": {
"access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0",
"token_type": "bearer",
"scope": "user:email"
}
}
你可以通过数组方式获取用户属性:
$user['id']; // 1472352
$user['nickname']; // "overtrue"
$user['name']; // "安正超"
$user['email']; // "[email protected]"
...
或者通过对象方式获取:
$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();
$user->getOriginal();
$user->getToken();// or $user->getAccessToken()
$user->getProviderName(); // GitHub/Google/Facebook...
$user->getOriginal()
$user->getToken()
$user->getAccessToken()
$user['token']
$accessToken = new AccessToken(['access_token' => $accessToken]);
$user = $socialite->user($accessToken);
MIT