PHP code example of liliuwei / thinkphp-social
1. Go to this page and download the library: Download liliuwei/thinkphp-social library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
liliuwei / thinkphp-social example snippets
composer
// 安装之后会在config目录里自动生成social.php配置文件
return [
//腾讯QQ登录配置
'qq' => [
'app_key' => '*******', //应用注册成功后分配的 APP ID
'app_secret' => '*******', //应用注册成功后分配的KEY
'callback' => 'http://www.youquanya.com/oauth/callback/type/qq', // 应用回调地址
],
//微信扫码登录配置
'weixin' => [
'app_key' => '*******', //应用注册成功后分配的 APP ID
'app_secret' => '*******', //应用注册成功后分配的KEY
'callback' => 'http://www.youquanya.com/oauth/callback/type/weixin', // 应用回调地址
],
];
//设置路由
Route::get('oauth/callback','index/oauth/callback');
namespace app\index\controller;
use think\Controller;
class Oauth extends Controller
{
//登录地址
public function login($type = null)
{
if ($type == null) {
$this->error('参数错误');
}
// 获取对象实例
$sns = \liliuwei\social\Oauth::getInstance($type);
//跳转到授权页面
$this->redirect($sns->getRequestCodeURL());
}
//授权回调地址
public function callback($type = null, $code = null)
{
if ($type == null || $code == null) {
$this->error('参数错误');
}
$sns = \liliuwei\social\Oauth::getInstance($type);
// 获取TOKEN
$token = $sns->getAccessToken($code);
//获取当前第三方登录用户信息
if (is_array($token)) {
$user_info = \liliuwei\social\GetInfo::getInstance($type, $token);
dump($user_info);// 获取第三方用户资料
$sns->openid();//统一使用$sns->openid()获取openid
//$sns->unionid();//QQ和微信、淘宝可以获取unionid
dump($sns->openid());
echo '登录成功!!';
echo '正在持续开发中,敬请期待!!';
} else {
echo "获取第三方用户的基本信息失败";
}
}
}