cxfoot/cxe/wechat/src/Wechat/WebOauth.php
2023-10-27 14:25:12 +08:00

51 lines
1.8 KiB
PHP

<?php
/**
* @author Any
* @description Do it yourself.
* @date 2018-6-21 21:45:31
* @version 1.0.0
*/
namespace Wechat;
class WebOauth extends Base
{
private $authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize';
private $tokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token';
private $refreshTokenUrl = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
private $userInfoUrl = 'https://api.weixin.qq.com/sns/userinfo';
public function getCode($scope,$redirectUrl)
{
$scope = trim($scope);
$authUrl = $this->authUrl .'?appid='.$this->wechat->appId.'&redirect_uri=' . $redirectUrl . '&scope='.$scope.'&response_type=code&state=1#wechat_redirect';
return $authUrl;
}
//获得access_token
public function getAccessToken($code) {
$tokenUrl = $this->tokenUrl.'?appid='.$this->wechat->appId.'&secret='.$this->wechat->appSecret.'&code='.$code.'&grant_type=authorization_code';
$this->wechat->curl->get($tokenUrl);
$res = json_decode($this->wechat->curl->response, true);
return $res;
}
//获得用户信息
public function getUserInfo($access_token,$openid) {
$userInfoUrl = $this->userInfoUrl.'?access_token='.$access_token.'&openid='.$openid;
$this->wechat->curl->get($userInfoUrl);
$res = json_decode($this->wechat->curl->response, true);
return $res;
}
//refresh_token
public function getRefreshToken($refreshToken) {
$refreshTokenUrl = $this->refreshTokenUrl.'?appid='.$this->wechat->appId.'&grant_type=refresh_token&refresh_token='.$refreshToken;
$this->wechat->curl->get($refreshTokenUrl);
$res = json_decode($this->wechat->curl->response, true);
return $res;
}
}