forked from thenbsp/wechat
/
OAuth.php
164 lines (132 loc) · 4.13 KB
/
OAuth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Thenbsp\Wechat;
use Thenbsp\Wechat\Util\Http;
use Thenbsp\Wechat\Exception\OAuthException;
/**
* 网页授权
* Created by thenbsp (thenbsp@gmail.com)
* Created at 2015/07/23
*/
class OAuth
{
/**
* 获取授权 URL 接口
*/
const AUTHORIZE_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize';
/**
* 获取用户 Token 接口
*/
const GET_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/access_token';
/**
* 刷新用户 Token 接口
*/
const REFRESH_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
/**
* 检测用户 AccessToken 是否有效接口
*/
const ACCESS_TOKEN_IS_VALID = 'https://api.weixin.qq.com/sns/auth';
/**
* 获取用户信息接口
*/
const USERINFO_URL = 'https://api.weixin.qq.com/sns/userinfo';
/**
* 公众号 APP ID
*/
protected $appid;
/**
* 公众号 APP SECRET
*/
protected $appsecret;
/**
* 构造方法
*/
function __construct($appid, $appsecret)
{
$this->appid = $appid;
$this->appsecret = $appsecret;
}
/**
* 直接跳转到授权页
*/
public function authorize($redirect_uri, $scope = 'snsapi_base', $state = null)
{
header('Location: '.$this->getAuthorizeURL($redirect_uri, $scope, $state));
}
/**
* 获取授权 URL (http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
*/
public function getAuthorizeURL($redirect_uri, $scope = 'snsapi_base', $state = null)
{
$params = [];
$params['appid'] = $this->appid;
$params['redirect_uri'] = $redirect_uri;
$params['response_type'] = 'code';
$params['scope'] = $scope;
$params['state'] = $state;
return static::AUTHORIZE_URL.'?'.http_build_query($params);
}
/**
* 授权回调(通过 Code 换取 Token)
*/
public function getToken($code)
{
$params = array(
'appid' => $this->appid,
'secret' => $this->appsecret,
'code' => $code,
'grant_type' => 'authorization_code'
);
$response = Http::get(static::GET_TOKEN_URL, $params);
if( isset($response->access_token) &&
isset($response->openid) ) {
return $response;
}
throw new OAuthException($response->errcode.': '.$response->errmsg);
}
/**
* 刷新 Token
*/
public function refreshToken($token)
{
$params = array(
'appid' => $this->appid,
'refresh_token' => $token->refresh_token,
'grant_type' => 'refresh_token'
);
$response = Http::get(static::REFRESH_TOKEN_URL, $params);
if( isset($response->access_token) &&
isset($response->openid) ) {
return $response;
}
throw new OAuthException($response->errcode.': '.$response->errmsg);
}
/**
* 检测用户 AccessToken 是否有效
*/
public function accessTokenIsValid($token)
{
$params = array(
'openid' => $token->openid,
'access_token' => $token->access_token
);
$response = Http::get(static::ACCESS_TOKEN_IS_VALID, $params);
return isset($response->errmsg) && ($response->errmsg === 'ok');
}
/**
* 获取当前(已授权)用户信息
*/
public function getUser($token)
{
$params = array(
'access_token' => $token->access_token,
'openid' => $token->openid
);
$response = Http::get(static::USERINFO_URL, $params);
if( isset($response->openid) &&
isset($response->nickname) &&
isset($response->headimgurl) ) {
return $response;
}
throw new OAuthException($response->errcode.': '.$response->errmsg);
}
}