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; |
38
|
|
|
protected $request = array(); |
39
|
|
|
protected $json = true; |
40
|
|
|
protected $accept; |
41
|
|
|
protected $curlInfo; |
42
|
|
|
protected $_curl_callback; |
43
|
|
|
|
44
|
|
|
public function __construct($api_key = "", $basic = "") |
45
|
|
|
{ |
46
|
|
|
if (!function_exists('curl_init')) |
47
|
|
|
{ |
48
|
|
|
throw new CurlException("cURL is not available. This API wrapper cannot be used."); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (isset($api_key)) |
52
|
|
|
{ |
53
|
|
|
$this->setApiKey($api_key); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($basic === true) |
57
|
|
|
{ |
58
|
|
|
$this->_api_key_var = 'Authorization: Basic '; |
59
|
|
|
$this->json = false; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setOAuth() |
64
|
|
|
{ |
65
|
|
|
$this->_api_key_var = ''; |
66
|
|
|
$this->json = false; |
67
|
|
|
$this->accept = true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function enableJson() |
71
|
|
|
{ |
72
|
|
|
$this->_api_key_var = 'Authorization: Bearer '; |
73
|
|
|
$this->json = true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setApiKey($api_key) |
77
|
|
|
{ |
78
|
|
|
$this->_api_key = $api_key; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function enableSslVerification() |
82
|
|
|
{ |
83
|
|
|
$this->_verify_ssl = true; |
84
|
|
|
$this->_verify_host = 2; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function disableSslVerification() |
88
|
|
|
{ |
89
|
|
|
$this->_verify_ssl = false; |
90
|
|
|
$this->_verify_host = 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function setTimeout($timeout) |
94
|
|
|
{ |
95
|
|
|
$this->_timeout = $timeout; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setCurlProxy($proxy) |
99
|
|
|
{ |
100
|
|
|
$this->curlProxy = $proxy; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setCurlCallback($callback) |
104
|
|
|
{ |
105
|
|
|
$this->_curl_callback = $callback; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function _call($url, $params = null, $headers = null, $method = "GET") |
109
|
|
|
{ |
110
|
|
|
$ch = curl_init(); |
111
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url); |
112
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_FOLLOWLOCATION, false); |
113
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_TIMEOUT, $this->_timeout); |
114
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true); |
115
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_SSL_VERIFYPEER, $this->_verify_ssl); |
116
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_SSL_VERIFYHOST, $this->_verify_host); |
117
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HEADER, true); |
118
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLINFO_HEADER_OUT, true); |
119
|
|
|
if ($this->curlProxy) { |
120
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_PROXY, $this->curlProxy); |
121
|
|
|
} |
122
|
|
|
if ($this->_curl_callback) { |
123
|
|
|
call_user_func($this->_curl_callback, $ch, $params, $headers, $method); |
124
|
|
|
} |
125
|
|
|
switch (strtoupper($method)) { |
126
|
|
|
case 'PUT': |
127
|
|
|
case 'PATCH': |
128
|
|
|
case 'DELETE': |
129
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); |
130
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_POSTFIELDS, json_encode($params)); |
131
|
|
|
break; |
132
|
|
|
case 'POST': |
133
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_POST, true); |
134
|
|
|
if ($this->json === true) { |
135
|
|
|
$params = json_encode($params); |
136
|
|
|
} |
137
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_POSTFIELDS, $params); |
138
|
|
|
break; |
139
|
|
|
case 'GET': |
140
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HTTPGET, true); |
141
|
|
|
if (!empty($params)) { |
142
|
|
|
$url .= '?'.http_build_query($params); |
143
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url); |
144
|
|
|
} |
145
|
|
|
break; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** old header set was here */ |
149
|
|
|
curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HTTPHEADER, $this->_createHeaders($headers = null)); |
|
|
|
|
150
|
|
|
|
151
|
|
|
$this->request['method'] = strtoupper($method); |
152
|
|
|
$this->request['headers'] = $this->_createHeaders($headers = null); |
|
|
|
|
153
|
|
|
$this->request['params'] = $params; |
154
|
|
|
|
155
|
|
|
$this->response = curl_exec(/** @scrutinizer ignore-type */ $ch); |
156
|
|
|
if (curl_errno(/** @scrutinizer ignore-type */ $ch)) { |
157
|
|
|
$this->curlErrno = curl_errno(/** @scrutinizer ignore-type */ $ch); |
158
|
|
|
$this->curlError = curl_error(/** @scrutinizer ignore-type */ $ch); |
159
|
|
|
curl_close(/** @scrutinizer ignore-type */ $ch); |
160
|
|
|
return; |
161
|
|
|
} |
162
|
|
|
$this->curlInfo = curl_getinfo(/** @scrutinizer ignore-type */ $ch); |
163
|
|
|
curl_close(/** @scrutinizer ignore-type */ $ch); |
164
|
|
|
return new Result($this->_getBody(), $this->_getHeaders(), $this->curlInfo); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private function _createHeaders($headers = null) |
168
|
|
|
{ |
169
|
|
|
$headers[] = $this->_api_key_var.$this->_api_key; |
170
|
|
|
if ($this->json === true) { |
171
|
|
|
$headers[] = 'Content-Type: application/json'; |
172
|
|
|
} |
173
|
|
|
if ($this->accept === true) { |
174
|
|
|
$headers[] = 'Accept: application/json'; |
175
|
|
|
} |
176
|
|
|
return $headers; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function _parseHeaders($raw_headers) |
180
|
|
|
{ |
181
|
|
|
if (!function_exists('http_parse_headers')) { |
182
|
|
|
$headers = array(); |
183
|
|
|
$key = ''; |
184
|
|
|
foreach (explode("\n", $raw_headers) as $i => $h) { |
185
|
|
|
$h = explode(':', $h, 2); |
186
|
|
|
if (isset($h[1])) { |
187
|
|
|
if (!isset($headers[$h[0]])) { |
188
|
|
|
$headers[$h[0]] = trim($h[1]); |
189
|
|
|
} elseif (is_array($headers[$h[0]])) { |
190
|
|
|
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); |
191
|
|
|
} else { |
192
|
|
|
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); |
193
|
|
|
} |
194
|
|
|
$key = $h[0]; |
195
|
|
|
} else { |
196
|
|
|
if (substr($h[0], 0, 1) == "\t") { |
197
|
|
|
$headers[$key] .= "\r\n\t".trim($h[0]); |
198
|
|
|
} elseif (!$key) { |
199
|
|
|
$headers[0] = trim($h[0]); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
return $headers; |
204
|
|
|
} else { |
205
|
|
|
return http_parse_headers($raw_headers); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
private function _getHeaders() |
210
|
|
|
{ |
211
|
|
|
return $this->_parseHeaders(substr($this->response, 0, $this->curlInfo['header_size'])); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
private function _getBody() |
215
|
|
|
{ |
216
|
|
|
return substr($this->response, $this->curlInfo['header_size']); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function getResponse() |
220
|
|
|
{ |
221
|
|
|
return $this->response; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function getRequest() |
225
|
|
|
{ |
226
|
|
|
return $this->request; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function get($url, $params = null, $headers = null) |
230
|
|
|
{ |
231
|
|
|
return $this->_call($url, $params, $headers, $method = "GET"); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function post($url, $params = null, $headers = null) |
235
|
|
|
{ |
236
|
|
|
return $this->_call($url, $params, $headers, $method = "POST"); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function delete($url, $params = null, $headers = null) |
240
|
|
|
{ |
241
|
|
|
return $this->_call($url, $params, $headers, $method = "DELETE"); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function put($url, $params = null, $headers = null) |
245
|
|
|
{ |
246
|
|
|
return $this->_call($url, $params, $headers, $method = "PUT"); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function patch($url, $params = null, $headers = null) |
250
|
|
|
{ |
251
|
|
|
return $this->_call($url, $params, $headers, $method = "PATCH"); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function getCurlInfo() |
255
|
|
|
{ |
256
|
|
|
return $this->curlInfo; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function isCurlError() |
260
|
|
|
{ |
261
|
|
|
return (bool) $this->curlErrno; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function getCurlErrno() |
265
|
|
|
{ |
266
|
|
|
return $this->curlErrno; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function getCurlError() |
270
|
|
|
{ |
271
|
|
|
return $this->curlError; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.