|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class WeChatOAuth {
|
|
|
|
|
|
|
|
|
|
// 获取微信授权的URL并跳转用户到微信授权页面
|
|
|
|
|
public function getCode($redirect_uri, $state=1, $scope='snsapi_base') {
|
|
|
|
|
// 处理 $redirect_uri,去掉开头的 '/' 以符合微信的要求
|
|
|
|
|
if (substr($redirect_uri, 0, 1) == '/') {
|
|
|
|
|
$redirect_uri = substr($redirect_uri, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 微信授权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";
|
|
|
|
|
|
|
|
|
|
// 使用 Location 头进行页面重定向,跳转到微信授权页面
|
|
|
|
|
header("Location: $url");
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 通过授权码获取 access_token 和 openid
|
|
|
|
|
public function getAccessTokenAndOpenId($code) {
|
|
|
|
|
// 构造微信API获取access_token的URL
|
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
|
|
|
|
|
. "appid=" . WECHAT_APPID
|
|
|
|
|
. "&secret=" . WECHAT_APPSECRET
|
|
|
|
|
. "&code=" . $code
|
|
|
|
|
. "&grant_type=authorization_code";
|
|
|
|
|
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
// 解析微信返回的数据(JSON格式)
|
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
|
|
|
|
|
|
// 判断请求是否成功
|
|
|
|
|
if (isset($res['access_token']) && isset($res['openid'])) {
|
|
|
|
|
return $res; // 返回access_token和openid
|
|
|
|
|
} else {
|
|
|
|
|
return false; // 请求失败返回false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用 refresh_token 刷新 access_token
|
|
|
|
|
public function refreshToken($refreshToken) {
|
|
|
|
|
// 构造微信API刷新access_token的URL
|
|
|
|
|
$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"
|
|
|
|
|
. "appid=" . WECHAT_APPID
|
|
|
|
|
. "&grant_type=refresh_token"
|
|
|
|
|
. "&refresh_token=" . $refreshToken;
|
|
|
|
|
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
// 解析返回的数据
|
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
|
|
|
|
|
|
// 判断请求是否成功
|
|
|
|
|
if (isset($res['access_token']) && isset($res['openid'])) {
|
|
|
|
|
return $res; // 返回新的access_token和openid
|
|
|
|
|
} else {
|
|
|
|
|
return false; // 请求失败返回false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用户的基本信息
|
|
|
|
|
public function getUserInfo($accessToken, $openId, $lang='zh_CN') {
|
|
|
|
|
// 构造微信API获取用户信息的URL
|
|
|
|
|
$url = "https://api.weixin.qq.com/sns/userinfo?"
|
|
|
|
|
. "access_token=" . $accessToken
|
|
|
|
|
. "&openid=" . $openId
|
|
|
|
|
. "&lang=" . $lang;
|
|
|
|
|
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
// 解析返回的JSON数据
|
|
|
|
|
return json_decode($result, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查 access_token 是否有效
|
|
|
|
|
public function checkAccessToken($accessToken, $openId) {
|
|
|
|
|
// 构造微信API验证access_token有效性的URL
|
|
|
|
|
$url = "https://api.weixin.qq.com/sns/auth?"
|
|
|
|
|
. "access_token=" . $accessToken
|
|
|
|
|
. "&openid=" . $openId;
|
|
|
|
|
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
// 解析返回的结果
|
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
|
|
|
|
|
|
// 如果返回值为 errcode 为 0,说明access_token有效
|
|
|
|
|
if ($res['errcode'] == 0) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|