1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Api.php |
4
|
|
|
################################################# |
5
|
|
|
## |
6
|
|
|
## PHPLicengine |
7
|
|
|
## |
8
|
|
|
################################################# |
9
|
|
|
## Copyright 2009-{current_year} PHPLicengine |
10
|
|
|
## |
11
|
|
|
## Licensed under the Apache License, Version 2.0 (the "License"); |
12
|
|
|
## you may not use this file except in compliance with the License. |
13
|
|
|
## You may obtain a copy of the License at |
14
|
|
|
## |
15
|
|
|
## http://www.apache.org/licenses/LICENSE-2.0 |
16
|
|
|
## |
17
|
|
|
## Unless required by applicable law or agreed to in writing, software |
18
|
|
|
## distributed under the License is distributed on an "AS IS" BASIS, |
19
|
|
|
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20
|
|
|
## See the License for the specific language governing permissions and |
21
|
|
|
## limitations under the License. |
22
|
|
|
################################################# |
23
|
|
|
|
24
|
|
|
namespace PHPLicengine\Api; |
25
|
|
|
use PHPLicengine\Exception\CurlException; |
26
|
|
|
|
27
|
|
|
class Api implements ApiInterface { |
28
|
|
|
|
29
|
|
|
protected $_api_key_var = 'Authorization: Bearer '; |
30
|
|
|
protected $_api_key; |
31
|
|
|
protected $_timeout = 30; |
32
|
|
|
protected $_verify_ssl = true; |
33
|
|
|
protected $_verify_host = 2; |
34
|
|
|
protected $curlErrno = false; |
35
|
|
|
protected $curlError = false; |
36
|
|
|
protected $curlProxy = false; |
37
|
|
|
protected $response = false; |
38
|
|
|
protected $request = array(); |
39
|
|
|
protected $json = true; |
40
|
|
|
protected $curlInfo; |
41
|
|
|
protected $_curl_callback; |
42
|
|
|
|
43
|
|
|
public function __construct($api_key = "", $basic = false) |
44
|
|
|
{ |
45
|
|
|
if (!function_exists('curl_init')) |
46
|
|
|
{ |
47
|
|
|
throw new CurlException ("cURL is not available. This API wrapper cannot be used."); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (isset($api_key)) |
51
|
|
|
{ |
52
|
|
|
$this->setApiKey($api_key); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($basic == true) |
56
|
|
|
{ |
57
|
|
|
$this->_api_key_var = 'Authorization: Basic '; |
58
|
|
|
$this->json = false; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function enableJson() |
63
|
|
|
{ |
64
|
|
|
$this->_api_key_var = 'Authorization: Bearer '; |
65
|
|
|
$this->json = true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function setApiKey($api_key) |
69
|
|
|
{ |
70
|
|
|
$this->_api_key = $api_key; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function enableSslVerification() |
74
|
|
|
{ |
75
|
|
|
$this->_verify_ssl = true; |
76
|
|
|
$this->_verify_host = 2; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function disableSslVerification() |
80
|
|
|
{ |
81
|
|
|
$this->_verify_ssl = false; |
82
|
|
|
$this->_verify_host = 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setTimeout($timeout) |
86
|
|
|
{ |
87
|
|
|
$this->_timeout = $timeout; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setCurlProxy($proxy) |
91
|
|
|
{ |
92
|
|
|
$this->curlProxy = $proxy; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setCurlCallback($callback) |
96
|
|
|
{ |
97
|
|
|
$this->_curl_callback = $callback; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function _call($url, $params = null, $headers = null, $method = "GET") |
101
|
|
|
{ |
102
|
|
|
$ch = curl_init(); |
103
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
|
|
|
104
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); |
105
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout); |
106
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
107
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_verify_ssl); |
108
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->_verify_host); |
109
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); |
110
|
|
|
curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
111
|
|
|
if ($this->curlProxy) { |
112
|
|
|
curl_setopt($ch, CURLOPT_PROXY, $this->curlProxy); |
113
|
|
|
} |
114
|
|
|
if ($this->_curl_callback) { |
115
|
|
|
call_user_func($this->_curl_callback, $ch, $params, $headers, $method); |
116
|
|
|
} |
117
|
|
|
switch (strtoupper($method)) { |
118
|
|
|
case 'PUT': |
119
|
|
|
case 'PATCH': |
120
|
|
|
case 'DELETE': |
121
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); |
122
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); |
123
|
|
|
break; |
124
|
|
|
case 'POST': |
125
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
126
|
|
|
if ($this->json === true) { |
127
|
|
|
$params = json_encode($params); |
128
|
|
|
} |
129
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); |
130
|
|
|
break; |
131
|
|
|
case 'GET': |
132
|
|
|
curl_setopt($ch, CURLOPT_HTTPGET, true); |
133
|
|
|
if (!empty($params)) { |
134
|
|
|
$url .= '?' . http_build_query($params); |
135
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
136
|
|
|
} |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$headers[] = $this->_api_key_var.$this->_api_key; |
141
|
|
|
if ($this->json === true) { |
142
|
|
|
$headers[] = 'Content-Type: application/json'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
146
|
|
|
|
147
|
|
|
$this->request['method'] = strtoupper($method); |
148
|
|
|
$this->request['headers'] = $headers; |
149
|
|
|
$this->request['params'] = $params; |
150
|
|
|
|
151
|
|
|
$this->response = curl_exec($ch); |
|
|
|
|
152
|
|
|
if (curl_errno($ch)) { |
|
|
|
|
153
|
|
|
$this->curlErrno = curl_errno($ch); |
154
|
|
|
$this->curlError = curl_error($ch); |
|
|
|
|
155
|
|
|
curl_close($ch); |
|
|
|
|
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
$this->curlInfo = curl_getinfo($ch); |
|
|
|
|
159
|
|
|
curl_close($ch); |
160
|
|
|
return new Result($this->_getBody(), $this->_getHeaders(), $this->curlInfo); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function _parseHeaders($raw_headers) |
164
|
|
|
{ |
165
|
|
|
if (!function_exists('http_parse_headers')) { |
166
|
|
|
$headers = array(); |
167
|
|
|
$key = ''; |
168
|
|
|
|
169
|
|
|
foreach(explode("\n", $raw_headers) as $i => $h) { |
170
|
|
|
$h = explode(':', $h, 2); |
171
|
|
|
|
172
|
|
|
if (isset($h[1])) { |
173
|
|
|
if (!isset($headers[$h[0]])) |
174
|
|
|
$headers[$h[0]] = trim($h[1]); |
175
|
|
|
elseif (is_array($headers[$h[0]])) { |
176
|
|
|
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); |
177
|
|
|
} |
178
|
|
|
else { |
179
|
|
|
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); |
180
|
|
|
} |
181
|
|
|
$key = $h[0]; |
182
|
|
|
} |
183
|
|
|
else { |
184
|
|
|
if (substr($h[0], 0, 1) == "\t") |
185
|
|
|
$headers[$key] .= "\r\n\t".trim($h[0]); |
186
|
|
|
elseif (!$key) |
187
|
|
|
$headers[0] = trim($h[0]); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
return $headers; |
191
|
|
|
|
192
|
|
|
} else { |
193
|
|
|
return http_parse_headers($raw_headers); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private function _getHeaders() |
198
|
|
|
{ |
199
|
|
|
return $this->_parseHeaders(substr($this->response, 0, $this->curlInfo['header_size'])); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
private function _getBody() |
203
|
|
|
{ |
204
|
|
|
return substr($this->response, $this->curlInfo['header_size']); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function getResponse() |
208
|
|
|
{ |
209
|
|
|
return $this->response; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getRequest() |
213
|
|
|
{ |
214
|
|
|
return $this->request; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function get($url, $params = null, $headers = null) |
218
|
|
|
{ |
219
|
|
|
return $this->_call($url, $params, $headers, $method = "GET"); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function post($url, $params = null, $headers = null) |
223
|
|
|
{ |
224
|
|
|
return $this->_call($url, $params, $headers, $method = "POST"); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function delete($url, $params = null, $headers = null) |
228
|
|
|
{ |
229
|
|
|
return $this->_call($url, $params, $headers, $method = "DELETE"); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function put($url, $params = null, $headers = null) |
233
|
|
|
{ |
234
|
|
|
return $this->_call($url, $params, $headers, $method = "PUT"); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function patch($url, $params = null, $headers = null) |
238
|
|
|
{ |
239
|
|
|
return $this->_call($url, $params, $headers, $method = "PATCH"); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function getCurlInfo() |
243
|
|
|
{ |
244
|
|
|
return $this->curlInfo; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function isCurlError () |
248
|
|
|
{ |
249
|
|
|
return (bool) $this->curlErrno; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function getCurlErrno () |
253
|
|
|
{ |
254
|
|
|
return $this->curlErrno; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function getCurlError () |
258
|
|
|
{ |
259
|
|
|
return $this->curlError; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
} |
263
|
|
|
|