-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcashive.php
310 lines (276 loc) · 8.32 KB
/
cashive.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
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
<?php
/**
* PHP SDK for cashive
*
* @author Deckmon Yang <[email protected]>
*/
namespace LibCashive;
class Response{
public $http_code;
public $response;
public $is_success;
function __construct($http_code, $response){
$this->http_code = $http_code;
$this->response = $response;
$this->is_success = ($http_code >= 200 and $http_code < 300 and $response["code"] == 0);
}
function get_errcode(){
return $this->response["code"];
}
function get_data(){
if(!$this->is_success){
return "";
}
return $this->response["data"];
}
function get_errmsg(){
if($this->is_success){
return "";
}
return $this->response["data"];
}
}
/**
* @author Deckmon
* @version 1.0
*/
class Client {
/**
* @ignore
*/
public $key;
/**
* @ignore
*/
public $secret;
/**
* Contains the last HTTP status code returned.
*
* @ignore
*/
public $http_code;
/**
* Contains the last API call.
*
* @ignore
*/
public $url;
/**
* Set up the API root URL.
*
* @ignore
*/
public $host = "http://cashive.mobishift.com/api/";
/**
* Set timeout default.
*
* @ignore
*/
public $timeout = 30;
/**
* Set connect timeout.
*
* @ignore
*/
public $connecttimeout = 30;
/**
* Verify SSL Cert.
*
* @ignore
*/
public $ssl_verifypeer = FALSE;
/**
* Contains the last HTTP headers returned.
*
* @ignore
*/
public $http_info;
/**
* Set the useragnet.
*
* @ignore
*/
public $useragent = 'Cashive PHP SDK v1.0';
/**
* boundary of multipart
* @ignore
*/
public static $boundary = '';
/**
* print the debug info
*
* @ignore
*/
public $debug = FALSE;
/**
* construct Cashive object
*/
function __construct($key, $secret) {
$this->key = $key;
$this->secret = $secret;
}
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
function makeSign($parameters){
ksort($parameters);
$pre_str = '';
foreach($parameters as $key => $value){
$pre_str = $pre_str.$key.'='.$value.'&';
}
$pre_str = $pre_str.'secret='.$this->secret;
$sign = md5($pre_str);
return $sign;
}
function signParams($parameters){
$new_params = array();
foreach($parameters as $key => $value){
if($value != ""){
$new_params[$key] = $value;
}
}
$new_params['rnd'] = $this->generateRandomString();
$new_params['time'] = (string) time();
$new_params['key'] = $this->key;
$sign = $this->makeSign($new_params);
$new_params['sign'] = $sign;
return $new_params;
}
function request($method, $url, $parameters=array()){
$url = "{$this->host}{$url}";
$parameters = $this->signParams($parameters);
$response = $this->MakeRequest($url, $method, $parameters);
$res = new Response($this->http_code, json_decode($response, true));
return $res;
}
/**
* Format and sign an OAuth / API request
*
* @return string
* @ignore
*/
function MakeRequest($url, $method, $parameters, $multi = false) {
switch ($method) {
case 'GET':
$url = $url . '?' . http_build_query($parameters);
return $this->http($url, 'GET');
default:
$headers = array();
if (!$multi && (is_array($parameters) || is_object($parameters)) ) {
$body = http_build_query($parameters);
} else {
$body = self::build_http_query_multi($parameters);
$headers[] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
}
return $this->http($url, $method, $body, $headers);
}
}
/**
* Make an HTTP request
*
* @return string API results
* @ignore
*/
function http($url, $method, $postfields = NULL, $headers = array()) {
$this->http_info = array();
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_ENCODING, "");
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
if (version_compare(phpversion(), '5.4.0', '<')) {
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 1);
} else {
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);
}
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
curl_setopt($ci, CURLOPT_HEADER, FALSE);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, TRUE);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
$this->postdata = $postfields;
}
break;
case 'DELETE':
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
if (!empty($postfields)) {
$url = "{$url}?{$postfields}";
}
}
curl_setopt($ci, CURLOPT_URL, $url );
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
$response = curl_exec($ci);
$this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
$this->http_info = array_merge($this->http_info, curl_getinfo($ci));
$this->url = $url;
if ($this->debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo "=====headers======\r\n";
print_r($headers);
echo '=====request info====='."\r\n";
print_r( curl_getinfo($ci) );
echo '=====response====='."\r\n";
print_r( $response );
}
curl_close ($ci);
return $response;
}
/**
* Get the header info to store.
*
* @return int
* @ignore
*/
function getHeader($ch, $header) {
$i = strpos($header, ':');
if (!empty($i)) {
$key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
$value = trim(substr($header, $i + 2));
$this->http_header[$key] = $value;
}
return strlen($header);
}
/**
* @ignore
*/
public static function build_http_query_multi($params) {
if (!$params) return '';
uksort($params, 'strcmp');
$pairs = array();
self::$boundary = $boundary = uniqid('------------------');
$MPboundary = '--'.$boundary;
$endMPboundary = $MPboundary. '--';
$multipartbody = '';
foreach ($params as $parameter => $value) {
if( in_array($parameter, array('pic', 'image')) && $value{0} == '@' ) {
$url = ltrim( $value, '@' );
$content = file_get_contents( $url );
$array = explode( '?', basename( $url ) );
$filename = $array[0];
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"'. "\r\n";
$multipartbody .= "Content-Type: image/unknown\r\n\r\n";
$multipartbody .= $content. "\r\n";
} else {
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";
$multipartbody .= $value."\r\n";
}
}
$multipartbody .= $endMPboundary;
return $multipartbody;
}
}