|
|
|
@ -1,69 +1,104 @@
|
|
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
namespace LaneWeChat\Core;
|
|
|
|
|
// WeChatOAuth类用于处理微信OAuth认证流程。
|
|
|
|
|
class WeChatOAuth{
|
|
|
|
|
// getCode静态方法用于生成用户授权的URL,并重定向用户到微信授权页面。
|
|
|
|
|
public static function getCode($redirect_uri, $state=1, $scope='snsapi_base'){
|
|
|
|
|
// 如果redirect_uri以'/'开头,则去掉它。
|
|
|
|
|
if($redirect_uri[0] == '/'){
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从配置中获取appid。
|
|
|
|
|
$appid = WECHAT_APPID;
|
|
|
|
|
// 微信授权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";
|
|
|
|
|
|
|
|
|
|
// 将redirect_uri添加到WECHAT_URL后进行URL编码。
|
|
|
|
|
$redirect_uri = WECHAT_URL . $redirect_uri;
|
|
|
|
|
$redirect_uri = urlencode($redirect_uri);
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
// 设置response_type为code,表示期望的响应类型是授权码。
|
|
|
|
|
$response_type = 'code';
|
|
|
|
|
// 解析微信返回的数据(JSON格式)
|
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
|
|
|
|
|
|
// 构造微信授权URL。
|
|
|
|
|
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appid . '&redirect_uri=' . $redirect_uri . '&response_type=' . $response_type . '&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
|
|
|
|
|
// 发送HTTP重定向响应到微信授权URL。
|
|
|
|
|
header('Location: ' . $url, true, 301);
|
|
|
|
|
// 判断请求是否成功
|
|
|
|
|
if (isset($res['access_token']) && isset($res['openid'])) {
|
|
|
|
|
return $res; // 返回access_token和openid
|
|
|
|
|
} else {
|
|
|
|
|
return false; // 请求失败返回false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getAccessTokenAndOpenId静态方法用于通过授权码获取access_token和openid。
|
|
|
|
|
public static function getAccessTokenAndOpenId($code){
|
|
|
|
|
// 使用 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;
|
|
|
|
|
|
|
|
|
|
// 设置grant_type为authorization_code,表示使用授权码换取access_token。
|
|
|
|
|
$grant_type = 'authorization_code';
|
|
|
|
|
// 调用自定义的Curl方法,发送请求到微信API
|
|
|
|
|
$result = Curl::callWebServer($url, null);
|
|
|
|
|
|
|
|
|
|
// 构造获取access_token的URL。
|
|
|
|
|
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . WECHAT_APPID . '&secret=' . WECHAT_APPSECRET . '&code=' . $code . '&grant_type=' . $grant_type;
|
|
|
|
|
// 解析返回的数据
|
|
|
|
|
$res = json_decode($result, true);
|
|
|
|
|
|
|
|
|
|
// 调用Curl类的方法发送请求并获取结果。
|
|
|
|
|
return Curl::callWebServer($url);
|
|
|
|
|
// 判断请求是否成功
|
|
|
|
|
if (isset($res['access_token']) && isset($res['openid'])) {
|
|
|
|
|
return $res; // 返回新的access_token和openid
|
|
|
|
|
} else {
|
|
|
|
|
return false; // 请求失败返回false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refreshToken静态方法用于刷新access_token。
|
|
|
|
|
public static function refreshToken($refreshToken){
|
|
|
|
|
// 构造刷新access_token的URL。
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . WECHAT_APPID . '&grant_type=refresh_token&refresh_token=' . $refreshToken;
|
|
|
|
|
$queryAction = 'GET';
|
|
|
|
|
// 调用Curl类的方法发送请求并获取结果。
|
|
|
|
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getUserInfo静态方法用于获取用户信息。
|
|
|
|
|
public static function getUserInfo($accessToken, $openId, $lang='zh_CN'){
|
|
|
|
|
// 构造获取用户信息的URL。
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $accessToken . '&openid=' . $openId . '&lang=' . $lang;
|
|
|
|
|
$queryAction = 'GET';
|
|
|
|
|
// 调用Curl类的方法发送请求并获取结果。
|
|
|
|
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
|
|
|
|
// 获取用户的基本信息
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checkAccessToken静态方法用于检查access_token是否有效。
|
|
|
|
|
public static function checkAccessToken($accessToken, $openId){
|
|
|
|
|
// 构造检查access_token的URL。
|
|
|
|
|
$queryUrl = 'https://api.weixin.qq.com/sns/auth?access_token=' . $accessToken . '&openid=' . $openId;
|
|
|
|
|
$queryAction = 'GET';
|
|
|
|
|
// 调用Curl类的方法发送请求并获取结果。
|
|
|
|
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
|
|
|
|
// 检查 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|