Update wechatoauth.lib.php

src
pvfho47bq 6 months ago
parent caf5d39b47
commit 598fef975e

@ -1,26 +1,32 @@
<?php
/**
* 微信OAuth2类
*
* 该类用于处理微信OAuth2授权流程包括获取授权URL、获取access_token和openid、刷新access_token、获取用户信息和检查access_token有效性。
*/
class WeChatOAuth {
/**
* 获取微信授权的URL并跳转用户到微信授权页面
*
* @param string $redirect_uri 授权后重定向的回调链接地址,需 urlencode 编码
* @param int $state 重定向后会带上 state 参数,可标识用户的不同身份
* @param string $scope 应用授权作用域,此处默认为 snsapi_base
* @return void 直接进行页面跳转,无返回值
*/
public function getCode($redirect_uri, $state=1, $scope='snsapi_base') {
public function getCode($redirect_uri, $state = 1, $scope = 'snsapi_base') {
// 确保 redirect_uri 符合微信要求,去除前导斜杠
$redirect_uri = ltrim($redirect_uri, '/');
// 构建微信授权URL拼接所有必要的参数
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?" .
"appid=" . WECHAT_APPID .
"&redirect_uri=" . urlencode("http://" . $_SERVER['HTTP_HOST'] . "/" . $redirect_uri) .
"&response_type=code" .
"&scope=" . $scope .
"&state=" . $state .
"#wechat_redirect";
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' .
'appid=' . WECHAT_APPID .
'&redirect_uri=' . urlencode("http://" . $_SERVER['HTTP_HOST'] . "/" . $redirect_uri) .
'&response_type=code' .
'&scope=' . $scope .
'&state=' . $state .
'#wechat_redirect';
// 使用 HTTP 头实现页面跳转跳至微信授权页面
header("Location: $url");
@ -29,16 +35,17 @@ class WeChatOAuth {
/**
* 通过授权码获取 access_token 和 openid
*
* @param string $code 授权回调时带来的授权码参数
* @return array|bool 返回包含 access_token 和 openid 的数组,或在失败时返回 false
*/
public function getAccessTokenAndOpenId($code) {
// 构建请求微信接口的URL用于获取 access_token 和 openid
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?" .
"appid=" . WECHAT_APPID .
"&secret=" . WECHAT_APPSECRET .
"&code=" . $code .
"&grant_type=authorization_code";
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?' .
'appid=' . WECHAT_APPID .
'&secret=' . WECHAT_APPSECRET .
'&code=' . $code .
'&grant_type=authorization_code';
// 发起请求,获取响应结果
$result = Curl::callWebServer($url, null);
@ -50,15 +57,16 @@ class WeChatOAuth {
/**
* 使用 refresh_token 刷新 access_token
*
* @param string $refreshToken 用于刷新的旧 access_token
* @return array|bool 成功时返回新的 access_token 和 openid失败时返回 false
*/
public function refreshToken($refreshToken) {
// 构建请求微信接口的URL用于刷新 access_token
$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?" .
"appid=" . WECHAT_APPID .
"&grant_type=refresh_token" .
"&refresh_token=" . $refreshToken;
$url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?' .
'appid=' . WECHAT_APPID .
'&grant_type=refresh_token' .
'&refresh_token=' . $refreshToken;
// 发起请求,获取响应结果
$result = Curl::callWebServer($url, null);
@ -70,17 +78,18 @@ class WeChatOAuth {
/**
* 获取用户的基本信息
*
* @param string $accessToken 有效的 access_token
* @param string $openId 用户标识
* @param string $lang 返回国家语言,这里默认为简体中文 'zh_CN'
* @return array 返回包含用户信息的数组
*/
public function getUserInfo($accessToken, $openId, $lang='zh_CN') {
public function getUserInfo($accessToken, $openId, $lang = 'zh_CN') {
// 构建请求微信接口的URL用于获取用户信息
$url = "https://api.weixin.qq.com/sns/userinfo?" .
"access_token=" . $accessToken .
"&openid=" . $openId .
"&lang=" . $lang;
$url = 'https://api.weixin.qq.com/sns/userinfo?' .
'access_token=' . $accessToken .
'&openid=' . $openId .
'&lang=' . $lang;
// 发起请求,获取响应结果
$result = Curl::callWebServer($url, null);
@ -91,15 +100,16 @@ class WeChatOAuth {
/**
* 检查 access_token 是否有效
*
* @param string $accessToken 需要检查的 access_token
* @param string $openId 用户标识
* @return bool 有效返回 true无效返回 false
*/
public function checkAccessToken($accessToken, $openId) {
// 构建请求微信接口的URL用于验证 access_token 有效性
$url = "https://api.weixin.qq.com/sns/auth?" .
"access_token=" . $accessToken .
"&openid=" . $openId;
$url = 'https://api.weixin.qq.com/sns/auth?' .
'access_token=' . $accessToken .
'&openid=' . $openId;
// 发起请求,获取响应结果
$result = Curl::callWebServer($url, null);

Loading…
Cancel
Save