网上有各种各样的三方登陆包,但真正好用的寥寥无几,要么配置太复杂,要么逻辑写得乱七八糟,经过一番查找在github上发现一个非常好用的三方扩展,现分享给大伙。
具体演示可以看我的站点: http://3d5156.com
1、先安装composer, PHP 世界的包管理工具,如果不会的,自行度娘,
2、去github上下载,https://github.com/anerg2046/sns_auth
3、下面以thinkphp5为例,实现第三方登陆。
3.1、 在index.php中包含上require '/vendor/autoload.php';
3.2、在application下新建一个模块api,在里面建立Sns.class.php
<?php
namespace app\api\controller;
use anerg\OAuth2\OAuth;
class Sns
{
private $config;
/**
* 第三方登录,执行跳转操作
*
* @param string $name 第三方渠道名称,目前可用的为:weixin,qq,weibo,alipay,facebook,twitter,line,google
*/
public function __construct() {
//获取配置
//这里以QQ登陆为例
$this->config = [];
$this->config['app_id'] = '1013****';
$this->config['app_secret'] = '67c52bc284b32e7**********';
$this->config['scope'] = 'get_user_info';
$this->config['callback'] = url('/sns/callback/' . $name, '', 'html', true); //回调地址
}
public function login($name)
{
return redirect(OAuth::$name($this->config)->getRedirectUrl());
}
/*
回调函数
*/
public function callback($name)
{
//获取格式化后的第三方用户信息
//$snsInfo = OAuth::$name($this->config)->userinfo();
//获取第三方返回的原始用户信息
$snsInfoRaw = OAuth::$name($this->config)->userinfoRaw();
//从这里开始分析$snsInfoRaw,跟数据库的会员表实现对接。。
//获取第三方openid
//$openid = OAuth::$name($this->config)->openid();
}
}具体演示可以看我的站点: http://3d5156.com