You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.9 KiB
47 lines
1.9 KiB
2 months ago
|
<?php
|
||
|
namespace LaneWeChat\Core;
|
||
|
class WeChatOAuth{
|
||
|
public static function getCode($redirect_uri, $state=1, $scope='snsapi_base'){
|
||
|
if($redirect_uri[0] == '/'){
|
||
|
$redirect_uri = substr($redirect_uri, 1);
|
||
|
}
|
||
|
|
||
|
$appid = WECHAT_APPID;
|
||
|
|
||
|
$redirect_uri = WECHAT_URL . $redirect_uri;
|
||
|
$redirect_uri = urlencode($redirect_uri);
|
||
|
|
||
|
$response_type = 'code';
|
||
|
|
||
|
$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';
|
||
|
header('Location: '.$url, true, 301);
|
||
|
}
|
||
|
|
||
|
|
||
|
public static function getAccessTokenAndOpenId($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='.$grant_type.'';
|
||
|
|
||
|
return Curl::callWebServer($url);
|
||
|
}
|
||
|
|
||
|
public static function refreshToken($refreshToken){
|
||
|
$queryUrl = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.WECHAT_APPID.'&grant_type=refresh_token&refresh_token='.$refreshToken;
|
||
|
$queryAction = 'GET';
|
||
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
||
|
}
|
||
|
|
||
|
public static function getUserInfo($accessToken, $openId, $lang='zh_CN'){
|
||
|
$queryUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token='. $accessToken . '&openid='. $openId .'&lang=zh_CN';
|
||
|
$queryAction = 'GET';
|
||
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
||
|
}
|
||
|
|
||
|
public static function checkAccessToken($accessToken, $openId){
|
||
|
$queryUrl = 'https://api.weixin.qq.com/sns/auth?access_token='.$accessToken.'&openid='.$openId;
|
||
|
$queryAction = 'GET';
|
||
|
return Curl::callWebServer($queryUrl, '', $queryAction);
|
||
|
}
|
||
|
}
|