-
Notifications
You must be signed in to change notification settings - Fork 14
/
Client.php
executable file
·201 lines (183 loc) · 5.97 KB
/
Client.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
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
<?php
/**
* DuoshuoSDK Client类定义
*
* @version $Id: Client.php 0 16:35 2013-4-11
* @author xiaowu
* @copyright Copyright (c) 2012 - , Duoshuo, Inc.
* @link http://dev.duoshuo.com
*/
require 'EasyHttp.php';
require 'EasyHttp/Curl.php';
require 'EasyHttp/Cookie.php';
require 'EasyHttp/Encoding.php';
require 'EasyHttp/Fsockopen.php';
require 'EasyHttp/Proxy.php';
require 'EasyHttp/Streams.php';
/**
*
* @link http://duoshuo.com/
* @author shen2
*
*/
class Duoshuo_Client{
var $end_point = 'http://api.duoshuo.com/';
/**
* 返回值格式
* @var string
*/
var $format = 'json';
var $userAgent = 'DuoshuoPhpSdk/0.3.0';
var $connecttimeout = 30;
var $timeout = 60;
var $shortName;
var $secret;
var $accessToken;
var $http;
function __construct($shortName = null, $secret = null, $remoteAuth = null, $accessToken = null){
$this->shortName = $shortName;
$this->secret = $secret;
$this->remoteAuth = $remoteAuth;
$this->accessToken = $accessToken;
}
function getLogList($params){
return $this->request('GET', 'log/list', $params);
}
/**
*
* @param $method
* @param $path
* @param $params
* @throws Duoshuo_Exception
* @return array
*/
function request($method, $path, $params = array()){
$params['short_name'] = $this->shortName;
$params['secret'] = $this->secret;
$params['remote_auth'] = $this->remoteAuth;
if ($this->accessToken)
$params['access_token'] = $this->accessToken;
$url = $this->end_point . $path. '.' . $this->format;
return $this->httpRequest($url, $method, $params);
}
function httpRequest($url, $method, $params){
$args = array(
'method' => $method, // GET/POST
'timeout' => $this->timeout, // 超时的秒数
'redirection' => 5, // 最大重定向次数
'httpversion' => '1.0', // 1.0/1.1
'user-agent' => $this->userAgent,
//'blocking' => true, // 是否阻塞
'headers' => array('Expect'=>''), // header信息
//'cookies' => array(), // 关联数组形式的cookie信息
//'compress' => false, // 是否压缩
//'decompress' => true, // 是否自动解压缩结果
'sslverify' => true,
//'stream' => false,
//'filename' => null // 如果stream = true,则必须设定一个临时文件名
);
switch($method){
case 'GET':
$url .= '?' . http_build_query($params, null, '&'); // overwrite arg_separator.output
break;
case 'POST':
$headers = array();
$args['body'] = http_build_query($params);
break;
default:
}
$http = new EasyHttp();
$response = $http->request($url, $args);
if (isset($response->errors)){
if (isset($response->errors['http_request_failed'])){
$message = $response->errors['http_request_failed'][0];
if ($message == 'name lookup timed out')
$message = 'DNS解析超时,请重试或检查你的主机的域名解析(DNS)设置。';
elseif (stripos($message, 'Could not open handle for fopen') === 0)
$message = '无法打开fopen句柄,请重试或联系多说管理员。http://dev.duoshuo.com/';
elseif (stripos($message, 'Couldn\'t resolve host') === 0)
$message = '无法解析duoshuo.com域名,请重试或检查你的主机的域名解析(DNS)设置。';
elseif (stripos($message, 'Operation timed out after ') === 0)
$message = '操作超时,请重试或联系多说管理员。http://dev.duoshuo.com/';
throw new Duoshuo_Exception($message, Duoshuo_Exception::REQUEST_TIMED_OUT);
}
else
throw new Duoshuo_Exception('连接服务器失败, 详细信息:' . json_encode($response->errors), Duoshuo_Exception::REQUEST_TIMED_OUT);
}
$json = json_decode($response['body'], true);
return $json === null ? $response['body'] : $json;
}
/**
*
* @param string $type
* @param array $keys
*/
function getAccessToken( $type, $keys ) {
$params = array(
'client_id' => $this->shortName,
'client_secret' => $this->secret,
);
switch($type){
case 'token':
$params['grant_type'] = 'refresh_token';
$params['refresh_token'] = $keys['refresh_token'];
break;
case 'code':
$params['grant_type'] = 'authorization_code';
$params['code'] = $keys['code'];
$params['redirect_uri'] = $keys['redirect_uri'];
break;
case 'password':
$params['grant_type'] = 'password';
$params['username'] = $keys['username'];
$params['password'] = $keys['password'];
break;
default:
throw new Duoshuo_Exception("wrong auth type");
}
$accessTokenUrl = 'http://api.duoshuo.com/oauth2/access_token';
$response = $this->httpRequest($accessTokenUrl, 'POST', $params);
$token = $response;
if ( is_array($token) && !isset($token['error']) ) {
$this->access_token = $token['access_token'];
if (isset($token['refresh_token'])) // 可能没有refresh_token
$this->refresh_token = $token['refresh_token'];
} else {
throw new Duoshuo_Exception("get access token failed." . $token['error']);
}
return $token;
}
/**
*
* @param array $user_data
*/
function remoteAuth($user_data){
$message = base64_encode(json_encode($user_data));
$time = time();
return $message . ' ' . self::hmacsha1($message . ' ' . $time, $this->secret) . ' ' . $time;
}
// from: http://www.php.net/manual/en/function.sha1.php#39492
// Calculate HMAC-SHA1 according to RFC2104
// http://www.ietf.org/rfc/rfc2104.txt
static function hmacsha1($data, $key) {
if (function_exists('hash_hmac'))
return hash_hmac('sha1', $data, $key,true);
$blocksize=64;
$hashfunc='sha1';
if (strlen($key)>$blocksize)
$key=pack('H*', $hashfunc($key));
$key=str_pad($key,$blocksize,chr(0x00));
$ipad=str_repeat(chr(0x36),$blocksize);
$opad=str_repeat(chr(0x5c),$blocksize);
$hmac = pack(
'H*',$hashfunc(
($key^$opad).pack(
'H*',$hashfunc(
($key^$ipad).$data
)
)
)
);
return bin2hex($hmac);
}
}