首页 > 代码库 > thinkphp 3.23 第三方登录sdk集成包
thinkphp 3.23 第三方登录sdk集成包
本集成包在官方包上扩展了支付宝登录和微信,支持最新的3.23版本
config
/* URL配置 */
‘URL_CASE_INSENSITIVE‘ => true, //默认false 表示URL区分大小写 true则表示不区分大小写
‘URL_MODEL‘ => 3, //URL模式
‘VAR_URL_PARAMS‘ => ‘‘, // PATHINFO URL参数变量
‘URL_PATHINFO_DEPR‘ => ‘/‘, //PATHINFO URL分割符
/* 加载扩展配置文件 */
‘LOAD_EXT_CONFIG‘ => ‘sdk_config‘,
sdk_config
<?php
// +----------------------------------------------------------------------
// | LTHINK [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://LTHINK.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 涛哥 <liangtao.gz@foxmail.com>
// +----------------------------------------------------------------------
// | sdk_config.php 2015-12-01
// +----------------------------------------------------------------------
//定义回调URL通用的URL
define(‘URL_CALLBACK‘, ‘http://‘ . $_SERVER[‘SERVER_NAME‘] . ‘?s=/User/Index/callback‘);
return array(
//支付宝登录
‘THINK_SDK_ALIPAY‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘alipay‘,
),
//微信登录
‘THINK_SDK_WEIXIN‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘weixin‘,
),
//腾讯QQ登录配置
‘THINK_SDK_QQ‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘qq‘,
),
//腾讯微博配置
‘THINK_SDK_TENCENT‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘tencent‘,
),
//新浪微博配置
‘THINK_SDK_SINA‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘sina‘,
),
//网易微博配置
‘THINK_SDK_T163‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘t163‘,
),
//人人网配置
‘THINK_SDK_RENREN‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘renren‘,
),
//360配置
‘THINK_SDK_X360‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘x360‘,
),
//豆瓣配置
‘THINK_SDK_DOUBAN‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘douban‘,
),
//Github配置
‘THINK_SDK_GITHUB‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘github‘,
),
//Google配置
‘THINK_SDK_GOOGLE‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘google‘,
),
//MSN配置
‘THINK_SDK_MSN‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘msn‘,
),
//点点配置
‘THINK_SDK_DIANDIAN‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘diandian‘,
),
//淘宝网配置
‘THINK_SDK_TAOBAO‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘taobao‘,
),
//百度配置
‘THINK_SDK_BAIDU‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘baidu‘,
),
//开心网配置
‘THINK_SDK_KAIXIN‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘kaixin‘,
),
//搜狐微博配置
‘THINK_SDK_SOHU‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => URL_CALLBACK . ‘sohu‘,
),
);
IndexController
<?php
// +----------------------------------------------------------------------
// | LTHINK [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://LTHINK.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 涛哥 <liangtao.gz@foxmail.com>
// +----------------------------------------------------------------------
// IndexAction.class.php 2013-02-25
namespace User\Controller;
use Think\Controller;
use LT\ThinkSDK\ThinkOauth;
Header(‘Content-Type: text/html; charset=utf-8‘);
class IndexController extends Controller
{
//SNS登录首页
public function index()
{
lt(URL_CALLBACK);
$this->display();
}
//登录地址
public function login($type = null)
{
empty($type) && $this->error(‘参数错误‘);
//加载ThinkOauth类并实例化一个对象
$sns = ThinkOauth::getInstance($type);
//跳转到授权页面
redirect($sns->getRequestCodeURL());
}
//授权回调地址
public function callback($type = null, $code = null)
{
(empty($type) || empty($code)) && $this->error(‘参数错误‘);
//加载ThinkOauth类并实例化一个对象
$sns = ThinkOauth::getInstance($type);
//腾讯微博需传递的额外参数
$extend = null;
if ($type == ‘tencent‘) {
$extend = array(‘openid‘ => $this->_get(‘openid‘), ‘openkey‘ => $this->_get(‘openkey‘));
}
//请妥善保管这里获取到的Token信息,方便以后API调用
//调用方法,实例化SDK对象的时候直接作为构造函数的第二个参数传入
//如: $qq = ThinkOauth::getInstance(‘qq‘, $token);
$token = $sns->getAccessToken($code, $extend);
//获取当前登录用户信息
if (is_array($token)) {
$user_info = A(‘Type‘, ‘Event‘)->$type($token);
echo("<h1>恭喜!使用 {$type} 用户登录成功</h1><br>");
echo("授权信息为:<br>");
dump($token);
echo("当前登录用户信息为:<br>");
dump($user_info);
}
}
}
复制
TypeEvent.class
<?php
// +----------------------------------------------------------------------
// | LTHINK [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://LTHINK.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 涛哥 <liangtao.gz@foxmail.com>
// +----------------------------------------------------------------------
// TypeEvent.class.php 2015-12-02
namespace User\Event;
use LT\ThinkSDK\ThinkOauth;
Header(‘Content-Type: text/html; charset=utf-8‘);
class TypeEvent
{
//登录成功,获取腾讯QQ用户信息
public function qq($token)
{
$qq = ThinkOauth::getInstance(‘qq‘, $token);
$data = $qq->call(‘user/get_user_info‘);
if ($data[‘ret‘] == 0) {
$userInfo[‘type‘] = ‘QQ‘;
$userInfo[‘name‘] = $data[‘nickname‘];
$userInfo[‘nick‘] = $data[‘nickname‘];
$userInfo[‘head‘] = $data[‘figureurl_2‘];
return $userInfo;
} else {
throw_exception("获取腾讯QQ用户信息失败:{$data[‘msg‘]}");
}
}
//登录成功,获取腾讯微博用户信息
public function tencent($token)
{
$tencent = ThinkOauth::getInstance(‘tencent‘, $token);
$data = $tencent->call(‘user/info‘);
if ($data[‘ret‘] == 0) {
$userInfo[‘type‘] = ‘TENCENT‘;
$userInfo[‘name‘] = $data[‘data‘][‘name‘];
$userInfo[‘nick‘] = $data[‘data‘][‘nick‘];
$userInfo[‘head‘] = $data[‘data‘][‘head‘];
return $userInfo;
} else {
throw_exception("获取腾讯微博用户信息失败:{$data[‘msg‘]}");
}
}
//登录成功,获取新浪微博用户信息
public function sina($token)
{
$sina = ThinkOauth::getInstance(‘sina‘, $token);
$data = $sina->call(‘users/show‘, "uid={$sina->openid()}");
if ($data[‘error_code‘] == 0) {
$userInfo[‘type‘] = ‘SINA‘;
$userInfo[‘name‘] = $data[‘name‘];
$userInfo[‘nick‘] = $data[‘screen_name‘];
$userInfo[‘head‘] = $data[‘avatar_large‘];
return $userInfo;
} else {
throw_exception("获取新浪微博用户信息失败:{$data[‘error‘]}");
}
}
//登录成功,获取网易微博用户信息
public function t163($token)
{
$t163 = ThinkOauth::getInstance(‘t163‘, $token);
$data = $t163->call(‘users/show‘);
if ($data[‘error_code‘] == 0) {
$userInfo[‘type‘] = ‘T163‘;
$userInfo[‘name‘] = $data[‘name‘];
$userInfo[‘nick‘] = $data[‘screen_name‘];
$userInfo[‘head‘] = str_replace(‘w=48&h=48‘, ‘w=180&h=180‘, $data[‘profile_image_url‘]);
return $userInfo;
} else {
throw_exception("获取网易微博用户信息失败:{$data[‘error‘]}");
}
}
//登录成功,获取人人网用户信息
public function renren($token)
{
$renren = ThinkOauth::getInstance(‘renren‘, $token);
$data = $renren->call(‘users.getInfo‘);
if (!isset($data[‘error_code‘])) {
$userInfo[‘type‘] = ‘RENREN‘;
$userInfo[‘name‘] = $data[0][‘name‘];
$userInfo[‘nick‘] = $data[0][‘name‘];
$userInfo[‘head‘] = $data[0][‘headurl‘];
return $userInfo;
} else {
throw_exception("获取人人网用户信息失败:{$data[‘error_msg‘]}");
}
}
//登录成功,获取360用户信息
public function x360($token)
{
$x360 = ThinkOauth::getInstance(‘x360‘, $token);
$data = $x360->call(‘user/me‘);
if ($data[‘error_code‘] == 0) {
$userInfo[‘type‘] = ‘X360‘;
$userInfo[‘name‘] = $data[‘name‘];
$userInfo[‘nick‘] = $data[‘name‘];
$userInfo[‘head‘] = $data[‘avatar‘];
return $userInfo;
} else {
throw_exception("获取360用户信息失败:{$data[‘error‘]}");
}
}
//登录成功,获取豆瓣用户信息
public function douban($token)
{
$douban = ThinkOauth::getInstance(‘douban‘, $token);
$data = $douban->call(‘user/~me‘);
if (empty($data[‘code‘])) {
$userInfo[‘type‘] = ‘DOUBAN‘;
$userInfo[‘name‘] = $data[‘name‘];
$userInfo[‘nick‘] = $data[‘name‘];
$userInfo[‘head‘] = $data[‘avatar‘];
return $userInfo;
} else {
throw_exception("获取豆瓣用户信息失败:{$data[‘msg‘]}");
}
}
//登录成功,获取Github用户信息
public function github($token)
{
$github = ThinkOauth::getInstance(‘github‘, $token);
$data = $github->call(‘user‘);
if (empty($data[‘code‘])) {
$userInfo[‘type‘] = ‘GITHUB‘;
$userInfo[‘name‘] = $data[‘login‘];
$userInfo[‘nick‘] = $data[‘name‘];
$userInfo[‘head‘] = $data[‘avatar_url‘];
return $userInfo;
} else {
throw_exception("获取Github用户信息失败:{$data}");
}
}
//登录成功,获取Google用户信息
public function google($token)
{
$google = ThinkOauth::getInstance(‘google‘, $token);
$data = $google->call(‘userinfo‘);
if (!empty($data[‘id‘])) {
$userInfo[‘type‘] = ‘GOOGLE‘;
$userInfo[‘name‘] = $data[‘name‘];
$userInfo[‘nick‘] = $data[‘name‘];
$userInfo[‘head‘] = $data[‘picture‘];
return $userInfo;
} else {
throw_exception("获取Google用户信息失败:{$data}");
}
}
//登录成功,获取Google用户信息
public function msn($token)
{
$msn = ThinkOauth::getInstance(‘msn‘, $token);
$data = $msn->call(‘me‘);
if (!empty($data[‘id‘])) {
$userInfo[‘type‘] = ‘MSN‘;
$userInfo[‘name‘] = $data[‘name‘];
$userInfo[‘nick‘] = $data[‘name‘];
$userInfo[‘head‘] = ‘微软暂未提供头像URL,请通过 me/picture 接口下载‘;
return $userInfo;
} else {
throw_exception("获取msn用户信息失败:{$data}");
}
}
//登录成功,获取点点用户信息
public function diandian($token)
{
$diandian = ThinkOauth::getInstance(‘diandian‘, $token);
$data = $diandian->call(‘user/info‘);
if (!empty($data[‘meta‘][‘status‘]) && $data[‘meta‘][‘status‘] == 200) {
$userInfo[‘type‘] = ‘DIANDIAN‘;
$userInfo[‘name‘] = $data[‘response‘][‘name‘];
$userInfo[‘nick‘] = $data[‘response‘][‘name‘];
$userInfo[‘head‘] = "https://api.diandian.com/v1/blog/{$data[‘response‘][‘blogs‘][0][‘blogUuid‘]}/avatar/144";
return $userInfo;
} else {
throw_exception("获取点点用户信息失败:{$data}");
}
}
//登录成功,获取淘宝网用户信息
public function taobao($token)
{
$taobao = ThinkOauth::getInstance(‘taobao‘, $token);
$fields = ‘user_id,nick,sex,buyer_credit,avatar,has_shop,vip_info‘;
$data = $taobao->call(‘taobao.user.buyer.get‘, "fields={$fields}");
if (!empty($data[‘user_buyer_get_response‘][‘user‘])) {
$user = $data[‘user_buyer_get_response‘][‘user‘];
$userInfo[‘type‘] = ‘TAOBAO‘;
$userInfo[‘name‘] = $user[‘user_id‘];
$userInfo[‘nick‘] = $user[‘nick‘];
$userInfo[‘head‘] = $user[‘avatar‘];
return $userInfo;
} else {
throw_exception("获取淘宝网用户信息失败:{$data[‘error_response‘][‘msg‘]}");
}
}
//登录成功,获取百度用户信息
public function baidu($token)
{
$baidu = ThinkOauth::getInstance(‘baidu‘, $token);
$data = $baidu->call(‘passport/users/getLoggedInUser‘);
if (!empty($data[‘uid‘])) {
$userInfo[‘type‘] = ‘BAIDU‘;
$userInfo[‘name‘] = $data[‘uid‘];
$userInfo[‘nick‘] = $data[‘uname‘];
$userInfo[‘head‘] = "http://tb.himg.baidu.com/sys/portrait/item/{$data[‘portrait‘]}";
return $userInfo;
} else {
throw_exception("获取百度用户信息失败:{$data[‘error_msg‘]}");
}
}
//登录成功,获取开心网用户信息
public function kaixin($token)
{
$kaixin = ThinkOauth::getInstance(‘kaixin‘, $token);
$data = $kaixin->call(‘users/me‘);
if (!empty($data[‘uid‘])) {
$userInfo[‘type‘] = ‘KAIXIN‘;
$userInfo[‘name‘] = $data[‘uid‘];
$userInfo[‘nick‘] = $data[‘name‘];
$userInfo[‘head‘] = $data[‘logo50‘];
return $userInfo;
} else {
throw_exception("获取开心网用户信息失败:{$data[‘error‘]}");
}
}
//登录成功,获取搜狐用户信息
public function sohu($token)
{
$sohu = ThinkOauth::getInstance(‘sohu‘, $token);
$data = $sohu->call(‘i/prv/1/user/get-basic-info‘);
if (‘success‘ == $data[‘message‘] && !empty($data[‘data‘])) {
$userInfo[‘type‘] = ‘SOHU‘;
$userInfo[‘name‘] = $data[‘data‘][‘open_id‘];
$userInfo[‘nick‘] = $data[‘data‘][‘nick‘];
$userInfo[‘head‘] = $data[‘data‘][‘icon‘];
return $userInfo;
} else {
throw_exception("获取搜狐用户信息失败:{$data[‘message‘]}");
}
}
}
复制
使用方式:
1、使用命名空间
use LT\ThinkSDK\ThinkOauth;
2、设置三方登录的类别并赋予一个变量
$type = ThinkOauth::getInstance(‘qq‘);
3、设置配置文件
‘THINK_SDK_(TYPE)‘ => array(
‘APP_KEY‘ => ‘‘, //应用注册成功后分配的 APP ID
‘APP_SECRET‘ => ‘‘, //应用注册成功后分配的KEY
‘CALLBACK‘ => ‘‘, //注册应用填写的callback
),
上文中的(TYPE)为设置的类别,其值目前有以下几个:
//支付宝登录 THINK_SDK_ALIPAY
//微信登录 THINK_SDK_WEIXIN
//腾讯QQ登录配置 THINK_SDK_QQ
// 用户基本信息API接口 user/get_user_info
//腾讯微博配置 THINK_SDK_TENCENT
// 用户基本信息API接口 user/info
//新浪微博配 THINK_SDK_SINA
// 用户基本信息API接口 users/show。附加参数:‘uid=‘.$obj->openid()
//网易微博配置 THINK_SDK_T163
// 用户基本信息API接口 users/show
//人人网配置 THINK_SDK_RENREN
// 用户基本信息API接口 users.getInfo
//360配置 THINK_SDK_X360
// 用户基本信息API接口 user/me
//豆瓣配置 THINK_SDK_DOUBAN
// 用户基本信息API接口 user/~me
//Github配置 THINK_SDK_GITHUB
// 用户基本信息API接口 user
//Google配置 THINK_SDK_GOOGLE
// 用户基本信息API接口 userinfo
//MSN配置 THINK_SDK_MSN
// 用户基本信息API接口 msn。附加参数:token
//点点配置 THINK_SDK_DIANDIAN
// 用户基本信息API接口 user/info
//淘宝网配置 THINK_SDK_TAOBAO
// 用户基本信息API接口 taobao.user.buyer.get。附加参数:‘fields=user_id,nick,sex,buyer_credit,avatar,has_shop,vip_info‘
//百度配置 THINK_SDK_BAIDU
// 用户基本信息API接口 passport/users/getLoggedInUser
// 注意,百度的头像位置是http://tb.himg.baidu.com/sys/portrait/item/{$data[‘portrait‘]}
//开心网配置 THINK_SDK_KAIXIN
// 用户基本信息API接口 users/me
//搜狐微博配置 THINK_SDK_SOHU
// 用户基本信息API接口 i/prv/1/user/get-basic-info
4、实例化一个登录页面
redirect($type->getRequestCodeURL());
这里的$type是第二部获取的结果
5、回调页面
$code = $this->get(‘code‘);
$type = ‘QQ‘;
$sns = ThinkOauth::getInstance($type);
//腾讯微博需传递的额外参数
$extend = null;
if($type == ‘tencent‘){
$extend = array(‘openid‘ => $this->_get(‘openid‘), ‘openkey‘ => $this->_get(‘openkey‘));
}
//请妥善保管这里获取到的Token信息,方便以后API调用
//调用方法,实例化SDK对象的时候直接作为构造函数的第二个参数传入
//如: $qq = ThinkOauth::getInstance(‘qq‘, $token);
$token = $sns->getAccessToken($code , $extend);
//获取当前登录用户信息
if(is_array($token)){
$data = http://www.mamicode.com/$sns->call(‘user/get_user_info‘);
if($data[‘ret‘] == 0){
$userInfo[‘type‘] = ‘QQ‘;
$userInfo[‘name‘] = $data[‘nickname‘];
$userInfo[‘nick‘] = $data[‘nickname‘];
$userInfo[‘head‘] = $data[‘figureurl_2‘];
// 此处的$userInfo就是需要的用户信息
} else {
E(‘获取腾讯QQ用户信息失败 : ‘.$data[‘msg‘]);
}
}
thinkphp 3.23 第三方登录sdk集成包