SocialLogin 是一个简单易用的跨框架第三方登陆库,专注和致力于解决第三方登陆的问题,无需再看官方手册,一个一个服务商去接入登陆服务,现在只需要使用 SocialLogin库,把接入的服务商appkey 和 appsecret填好,立即就可以使用,让你无需关心细节实现,专注实现业务逻辑。
如何安装 | Installation
composer require barbery/social-login:dev-master
如何使用 | Usage
在框架的控制器中,建立一个Action来统一处理第三方登陆,以下的例子是假设在user的控制器中添加一个socialLogin的Action来处理
use Barbery\SocialLogin\SocialLogin;
class User extend Controllers
public function socialLoginAction()
'dns' => '127.0.0.1:1080',
'type' => CURLPROXY_SOCKS5
'redirectUrl' => '/user/socialLogin?type=weibo',
'secret' => 'xxxxxxxxxxxx',
'redirectUrl' => '/user/socialLogin?type=qq',
'key' => 'xxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxx',
'redirectUrl' => '/user/socialLogin?type=google',
'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
'secret' => 'xxxxxxxxxxxxx',
'redirectUrl' => '/user/socialLogin?type=github',
$obj = new SocialLogin($config);
$socialInfo = $obj->authAndGetUserInfo();
echo $e->getMessage(), '具体原因:' . print_r($obj->getLastError(), true);
if ( ! User::isExist($socialInfo['openid'], $socialInfo['from'])) {
redirect($_GET['redirectUrl']);
配好后,如果用户需要第三方登陆,只需要引导用户跳转到/user/socialInfo?type={$type}&redirectUrl=/index下就可以了,$type为你传进去的配置,例如,你只设置了google和github的登陆,type就只能是google或github,redirectUrl参数是当用户成功登陆后, 跳回地址。
配置说明 | Config
目前支持的第三方登陆有:Github,Google,QQ,Weibo,后续会不断增加,支持的列表可以从
这里看到。
配置demo:
'dns' => '127.0.0.1:1080',
'type' => CURLPROXY_SOCKS5
'secret' => 'xxxxxxxxxxxxxxx',
'redirectUrl' => '/user/socialLogin?type=weibo',
'secret' => 'xxxxxxxxxxxxxxx',
'redirectUrl' => 'http://cvmeta.com/user/socialLogin?type=qq',
'key' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxx',
'redirectUrl' => '/user/socialLogin?type=google',
'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
'key' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxx',
'redirectUrl' => '/user/socialLogin?type=github',
文档 | Document
private function getInstance()
获取sdk对象
public function authAndGetUserInfo()
引导用户授权且获取用户信息,该方法统一返回一个userInfo数据结构 'face' => '第三方头像url,优先拿大图',
'openid' => '第三方唯一身份标识id',
'access_token' => '授权access_token',
'timeout' => 'access_token过期时间戳',
'refresh_token' => 'refresh_token','如果有则返回,无则为空字符串',
public function getAuthorizeURL()
获取第三方授权地址
public function getFullUserInfo()
获取全部返回的用户信息
public function getLastError()
获取返回的最后的一个错误信息
'code' => '第三方登陆服务商返回错误码',
'msg' => '第三方登陆服务商返回的错误msg'
原文地址:https://blog.csdn.net/zhwxl_zyx/article/details/50293979
评论
发表评论