PHP code example of sujun / oauthsdk
1. Go to this page and download the library: Download sujun/oauthsdk 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/ */
sujun / oauthsdk example snippets
composer
" "sujun/oauthsdk": "v1.0.1"
}
use OauthSDK\Oauth;
try {
if (!in_array($_GET['type'], ['qq', 'wechat', 'sina'])) {
throw new \Exception("暂不支持{$_GET['type']}方式登录", 500);
}
$sns = Oauth::getInstance($_GET['type'], array(
//腾讯QQ登录配置
'QQ' => array(
'APP_KEY' => '***', //应用注册成功后分配的 APP ID
'APP_SECRET' => '***', //应用注册成功后分配的KEY
'CALLBACK' => '/callback.php?type=qq', //回调URL
),
//新浪微博配置
'SINA' => array(
'APP_KEY' => '***',
'APP_SECRET' => '***',
'CALLBACK' => '/callback.php?type=sina',
),
//腾讯微信配置
'WECHAT' => array(
'APP_KEY' => '***',
'APP_SECRET' => '***',
'CALLBACK' => '/callback.php?type=wechat',
),
));
//跳转到授权页面
header('Location: ' . $sns->getRequestCodeURL());
} catch (\Exception $e) {
echo $e->getMessage();
header('Location: /login' . );
}
use OauthSDK\Oauth;
try {
$type = $_GET['type'];
$code = $_GET['code'];
if (!in_array($type, ['qq', 'wechat', 'sina'])) {
throw new \Exception("参数错误", 500);
}
if(empty($type) || empty($code)) {
throw new \Exception('参数错误~',500);
}
$sns = Oauth::getInstance($type, array(
//腾讯QQ登录配置
'QQ' => array(
'APP_KEY' => '***', //应用注册成功后分配的 APP ID
'APP_SECRET' => '***', //应用注册成功后分配的KEY
'CALLBACK' => '/callback.php?type=qq', //回调URL
),
//新浪微博配置
'SINA' => array(
'APP_KEY' => '***',
'APP_SECRET' => '***',
'CALLBACK' => '/callback.php?type=sina',
),
//腾讯微信配置
'WECHAT' => array(
'APP_KEY' => '***',
'APP_SECRET' => '***',
'CALLBACK' => '/callback.php?type=wechat',
),
));
$tokenArr = $sns->getAccessToken($code, []);
$openid = $tokenArr['openid'];
$token = $tokenArr['access_token'];
/*
$checkData = select * from where openid=$tokenArr['openid'] and type_name = $type;
if ($_check_data) {
//之前绑定
//跳至登录
} */
//获取当前登录用户信息
if ($openid) {
$userinfo = $sns->getUserInfo();
//处理......
} else {
throw new \Exception("系统出错,请稍后再试!", 500);
}
} catch (\Exception $e) {
echo $e->errorMessage();
exit;
}