xbing2002 / socialite
A collection of OAuth 2 packages that extracts from laravel/socialite.
Fund package maintenance!
Patreon
Requires
- php: >=7.1
- easyswoole/cache: ^1.1
- easyswoole/http: ^1.3
- easyswoole/http-client: ^1.3
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-10 19:18:31 UTC
README
基于 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
有些登录平台可以在跳转之前设置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 interface
Standard user api:
$user = $socialite->driver('weibo')->user();
{ "id": 1472352, "nickname": "overtrue", "name": "安正超", "email": "anzhengchao@gmail.com", "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']; // "anzhengchao@gmail.com" ...
或者通过对象方式获取:
$user->getId(); $user->getNickname(); $user->getName(); $user->getEmail(); $user->getAvatar(); $user->getOriginal(); $user->getToken();// or $user->getAccessToken() $user->getProviderName(); // GitHub/Google/Facebook...
你也可以直接获取各登录平台的原始返回数据
$user->getOriginal()
获取access token 对象
$user->getToken()
$user->getAccessToken()
$user['token']
可以通过access token 获取用户信息
$accessToken = new AccessToken(['access_token' => $accessToken]); $user = $socialite->user($accessToken);
License
MIT