|
|
@ -2,108 +2,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
class WeChatOAuth {
|
|
|
|
class WeChatOAuth {
|
|
|
|
|
|
|
|
|
|
|
|
// 获取微信授权的URL并跳转用户到微信授权页面
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取微信授权的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 符合微信要求,去除前导斜杠
|
|
|
|
if (substr($redirect_uri, 0, 1) == '/') {
|
|
|
|
$redirect_uri = ltrim($redirect_uri, '/');
|
|
|
|
$redirect_uri = substr($redirect_uri, 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
// 构建微信授权URL,拼接所有必要的参数
|
|
|
|
|
|
|
|
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?" .
|
|
|
|
// 微信授权URL模板
|
|
|
|
"appid=" . WECHAT_APPID .
|
|
|
|
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
|
|
|
|
"&redirect_uri=" . urlencode("http://" . $_SERVER['HTTP_HOST'] . "/" . $redirect_uri) .
|
|
|
|
. "appid=" . WECHAT_APPID
|
|
|
|
"&response_type=code" .
|
|
|
|
. "&redirect_uri=" . urlencode("http://" . $_SERVER['HTTP_HOST'] . "/" . $redirect_uri)
|
|
|
|
"&scope=" . $scope .
|
|
|
|
. "&response_type=code&scope=" . $scope
|
|
|
|
"&state=" . $state .
|
|
|
|
. "&state=" . $state . "#wechat_redirect";
|
|
|
|
"#wechat_redirect";
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 Location 头进行页面重定向,跳转到微信授权页面
|
|
|
|
// 使用 HTTP 头实现页面跳转跳至微信授权页面
|
|
|
|
header("Location: $url");
|
|
|
|
header("Location: $url");
|
|
|
|
exit;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 通过授权码获取 access_token 和 openid
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 通过授权码获取 access_token 和 openid
|
|
|
|
|
|
|
|
* @param string $code 授权回调时带来的授权码参数
|
|
|
|
|
|
|
|
* @return array|bool 返回包含 access_token 和 openid 的数组,或在失败时返回 false
|
|
|
|
|
|
|
|
*/
|
|
|
|
public function getAccessTokenAndOpenId($code) {
|
|
|
|
public function getAccessTokenAndOpenId($code) {
|
|
|
|
// 构造微信API获取access_token的URL
|
|
|
|
// 构建请求微信接口的URL,用于获取 access_token 和 openid
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?" .
|
|
|
|
. "appid=" . WECHAT_APPID
|
|
|
|
"appid=" . WECHAT_APPID .
|
|
|
|
. "&secret=" . WECHAT_APPSECRET
|
|
|
|
"&secret=" . WECHAT_APPSECRET .
|
|
|
|
. "&code=" . $code
|
|
|
|
"&code=" . $code .
|
|
|
|
. "&grant_type=authorization_code";
|
|
|
|
"&grant_type=authorization_code";
|
|
|
|
|
|
|
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
// 发起请求,获取响应结果
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
|
|
// 解析微信返回的数据(JSON格式)
|
|
|
|
// 解析响应结果,检查是否获取成功
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
|
|
|
|
return isset($res['access_token'], $res['openid']) ? $res : false;
|
|
|
|
// 判断请求是否成功
|
|
|
|
|
|
|
|
if (isset($res['access_token']) && isset($res['openid'])) {
|
|
|
|
|
|
|
|
return $res; // 返回access_token和openid
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false; // 请求失败返回false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 refresh_token 刷新 access_token
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 使用 refresh_token 刷新 access_token
|
|
|
|
|
|
|
|
* @param string $refreshToken 用于刷新的旧 access_token
|
|
|
|
|
|
|
|
* @return array|bool 成功时返回新的 access_token 和 openid,失败时返回 false
|
|
|
|
|
|
|
|
*/
|
|
|
|
public function refreshToken($refreshToken) {
|
|
|
|
public function refreshToken($refreshToken) {
|
|
|
|
// 构造微信API刷新access_token的URL
|
|
|
|
// 构建请求微信接口的URL,用于刷新 access_token
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?" .
|
|
|
|
. "appid=" . WECHAT_APPID
|
|
|
|
"appid=" . WECHAT_APPID .
|
|
|
|
. "&grant_type=refresh_token"
|
|
|
|
"&grant_type=refresh_token" .
|
|
|
|
. "&refresh_token=" . $refreshToken;
|
|
|
|
"&refresh_token=" . $refreshToken;
|
|
|
|
|
|
|
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
// 发起请求,获取响应结果
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
|
|
// 解析返回的数据
|
|
|
|
// 解析响应结果,检查是否成功刷新
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
|
|
|
|
return isset($res['access_token'], $res['openid']) ? $res : false;
|
|
|
|
// 判断请求是否成功
|
|
|
|
|
|
|
|
if (isset($res['access_token']) && isset($res['openid'])) {
|
|
|
|
|
|
|
|
return $res; // 返回新的access_token和openid
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false; // 请求失败返回false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取用户的基本信息
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取用户的基本信息
|
|
|
|
|
|
|
|
* @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') {
|
|
|
|
// 构造微信API获取用户信息的URL
|
|
|
|
// 构建请求微信接口的URL,用于获取用户信息
|
|
|
|
$url = "https://api.weixin.qq.com/sns/userinfo?"
|
|
|
|
$url = "https://api.weixin.qq.com/sns/userinfo?" .
|
|
|
|
. "access_token=" . $accessToken
|
|
|
|
"access_token=" . $accessToken .
|
|
|
|
. "&openid=" . $openId
|
|
|
|
"&openid=" . $openId .
|
|
|
|
. "&lang=" . $lang;
|
|
|
|
"&lang=" . $lang;
|
|
|
|
|
|
|
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
// 发起请求,获取响应结果
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
|
|
// 解析返回的JSON数据
|
|
|
|
// 解析响应结果,直接返回
|
|
|
|
return json_decode($result, true);
|
|
|
|
return json_decode($result, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查 access_token 是否有效
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 检查 access_token 是否有效
|
|
|
|
|
|
|
|
* @param string $accessToken 需要检查的 access_token
|
|
|
|
|
|
|
|
* @param string $openId 用户标识
|
|
|
|
|
|
|
|
* @return bool 有效返回 true,无效返回 false
|
|
|
|
|
|
|
|
*/
|
|
|
|
public function checkAccessToken($accessToken, $openId) {
|
|
|
|
public function checkAccessToken($accessToken, $openId) {
|
|
|
|
// 构造微信API验证access_token有效性的URL
|
|
|
|
// 构建请求微信接口的URL,用于验证 access_token 有效性
|
|
|
|
$url = "https://api.weixin.qq.com/sns/auth?"
|
|
|
|
$url = "https://api.weixin.qq.com/sns/auth?" .
|
|
|
|
. "access_token=" . $accessToken
|
|
|
|
"access_token=" . $accessToken .
|
|
|
|
. "&openid=" . $openId;
|
|
|
|
"&openid=" . $openId;
|
|
|
|
|
|
|
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
// 发起请求,获取响应结果
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
|
|
// 解析返回的结果
|
|
|
|
// 解析响应结果,检查 errcode 是否为 0,表示 access_token 有效
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
|
|
|
|
return $res['errcode'] == 0;
|
|
|
|
// 如果返回值为 errcode 为 0,说明access_token有效
|
|
|
|
|
|
|
|
if ($res['errcode'] == 0) {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getCode方法:用于获取微信授权的URL并跳转用户到微信授权页面。处理 redirect_uri,构造微信授权URL,并使用 Location 头进行页面重定向。
|
|
|
|
|
|
|
|
getAccessTokenAndOpenId方法:通过授权码获取 access_token 和 openid。构造微信API获取 access_token 的URL,发送请求并解析返回的JSON数据。
|
|
|
|
|
|
|
|
refreshToken方法:使用 refresh_token 刷新 access_token。构造微信API刷新 access_token 的URL,发送请求并解析返回的JSON数据。
|
|
|
|
|
|
|
|
getUserInfo方法:获取用户的基本信息。构造微信API获取用户信息的URL,发送请求并解析返回的JSON数据。
|
|
|
|
|
|
|
|
checkAccessToken方法:检查 access_token 是否有效。构造微信API验证 access_token 有效性的URL,发送请求并解析返回的结果。
|
|
|
|
|