DoubanClient.class.php
8.73 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
class DoubanClient {
/**
* @brief 豆瓣Oauth类词头
*/
const PREFIX = 'Douban';
/**
* @brief authorizeCode请求链接
*/
protected $authorizeUri = 'https://www.douban.com/service/auth2/auth';
/**
* @brief accessToken请求链接
*/
protected $accessUri = 'https://www.douban.com/service/auth2/token';
/**
* @brief api请求链接
*/
protected $apiUri = 'https://api.douban.com';
/**
* @brief 豆瓣应用public key
*/
protected $clientId;
/**
* @brief 豆瓣应用secret key
*/
protected $secret;
/**
* @brief callback链接
*/
protected $redirectUri;
/**
* @brief Api权限
*/
protected $scope;
/**
* @brief 返回类型,默认使用code
*/
protected $responseType;
/**
* @brief 用户授权码
*/
protected $authorizeCode;
/**
* @brief 储存返回的令牌(accessToken,refreshToken)
*/
protected $tokens;
/**
* @brief 通过authorizeCode获得的访问令牌
*/
protected $accessToken;
/**
* @brief 用于刷新accessToken
*/
protected $refreshToken;
/**
* @var 默认请求头信息
*/
protected $defaultHeader = array(
'Content_type: application/x-www-form-urlencoded'
);
/**
* @var 需授权的请求头
*/
protected $authorizeHeader;
/**
* @var curl默认设置
*/
protected $CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'simple-douban-oauth2-0.4',
);
/**
* @brief 初始化豆瓣OAUTH,设置相关参数
*
* @param string $client_id
* @param string $secret
* @param string $redirect_uri
* @param string $scope
* @param string $responseType
*
* @return void
*/
public function __construct($clientId, $secret, $redirectUri, $scope ='douban_basic_common', $responseType = 'code')
{
$this->clientId = $clientId;
$this->secret = $secret;
$this->redirectUri = $redirectUri;
$this->scope = $scope;
$this->responseType = $responseType;
// // API基类路径
// $basePath = dirname(__FILE__).'/api/DoubanBase.php';
// // 载入API基类
// try {
// $this->fileLoader($basePath);
// } catch(Exception $e) {
// echo 'Baseloader error:'.$e->getMessage();
// }
}
/**
* @brief 跳转到豆瓣用户授权页面,获取AuthorizeCode
*
* @return redirect
*/
public function requestAuthorizeCode()
{
// 获取AuthorizeCode请求链接
$authorizeUrl = $this->getAuthorizeUrl();
header('Location:'.$authorizeUrl);
}
/**
* @brief 设置AuthorizeCode
*
* @param string $authorizeCode
*
* @return void
*/
public function setAuthorizeCode($authorizeCode)
{
$this->authorizeCode = $authorizeCode;
}
/**
* @brief 通过AuthorizeCode获取accessToken
*
* @return string
*/
public function requestAccessToken()
{
// 获取accessToken请求链接
$accessUrl = $this->getAccessUrl();
$header = $this->defaultHeader;
$result = $this->curl($accessUrl, 'POST', $header);
$this->tokens = json_decode($result);
$this->refreshToken = $this->tokens->refresh_token;
$this->accessToken = $this->tokens->access_token;
}
/**
* @brief 获取token
*
* @return string
*/
public function getToken()
{
return $this->tokens;
}
/**
* @brief 获取accessToken
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* @brief 设置token
*
* @return string
*/
public function setAccessToken($token)
{
if (is_array($token) && isset($token['access_token'])) {
$this->accessToken = $token['access_token'];
}
}
/**
* @brief 生成豆瓣用户授权页面完整地址
*
* @return string
*/
public function getAuthorizeUrl()
{
$params = array(
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUri,
'response_type' => $this->responseType,
'scope' => $this->scope
);
return $this->authorizeUri.'?'.http_build_query($params);
}
/**
* @brief 生成豆瓣access_token完整获取链接
*
* @return string
*/
public function getAccessUrl()
{
$params = array(
'client_id' => $this->clientId,
'client_secret' => $this->secret,
'redirect_uri' => $this->redirectUri,
'grant_type' => 'authorization_code',
'code' => $this->authorizeCode,
);
return $this->accessUri.'?'.http_build_query($params);
}
/**
* @brief 请求豆瓣API,返回包含相关数据的对象
*
* @param object $API
* @param array $data
* @param boolean 为true时会在header中发送accessToken
*
* @return object
*/
public function api($api, $type = 'POST', $data = null, $authorization = false)
{
// API的完整URL
$url = $this->apiUri.$api;
$header = $authorization ? $this->getAuthorizeHeader() : $this->defaultHeader;
$response = $this->curl($url, $type, $header, $data);
$result = json_decode($response, true);
return $result;
}
/**
* @brief 请求豆瓣API,返回包含相关数据的对象
*
* @param object $API
* @param array $data
* @param boolean 为true时会在header中发送accessToken
*
* @return object
*/
public function makeRequest($api, $data = null, $authorization = false)
{
// API的完整URL
$url = $this->apiUri.$api->uri;
$header = $authorization ? $this->getAuthorizeHeader() : $this->defaultHeader;
$type = $api->type;
return $this->curl($url, $type, $header, $data);
}
/**
* @brief 豆瓣API实例注册函数
*
* @param string $api
*
* @return object
*/
public function apiRegister($api)
{
$doubanApi = self::PREFIX.ucfirst(strtolower($api));
// 需要注册的API路径
$apiPath = dirname(__FILE__).'/api/'.$doubanApi.'.php';
try {
$this->fileLoader($apiPath);
} catch(Exception $e) {
echo 'Apiloader error:'.$e->getMessage();
}
return new $doubanApi($this->clientId);
}
/**
* @brief 获取Authorization header
*
* @return array
*/
protected function getAuthorizeHeader()
{
return $this->authorizeHeader = array('Authorization: Bearer '.$this->accessToken);
}
/**
* @brief 使用CURL模拟请求,并返回取得的数据
*
* @param string $url
* @param string $type
* @param array $header
* @param array $data
*
* @return object
*/
protected function curl($url, $type, $header, $data = null)
{
$opts = $this->CURL_OPTS;
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_CUSTOMREQUEST] = $type;
$header[] = 'Expect:';
$opts[CURLOPT_HTTPHEADER] = $header;
if ($type == 'POST' || $type =='PUT') {
$opts[CURLOPT_POSTFIELDS] = $data;
}
$ch = curl_init();
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
// if (curl_errno($ch)) {
// die('CURL error: '.curl_error($ch));
// }
curl_close($ch);
return $result;
}
/**
* @brief 文件加载类
*
* @param string $path
*
* @return void
*/
protected function fileLoader($path)
{
// 文件路径错误时抛出异常
if ( ! file_exists($path)) {
throw new Exception('The file you wanted to load does not exists.');
}
require $path;
}
}