1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WooCommerce REST API HTTP Client |
4
|
|
|
* |
5
|
|
|
* @category HttpClient |
6
|
|
|
* @package Automattic/WooCommerce |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Automattic\WooCommerce\HttpClient; |
10
|
|
|
|
11
|
|
|
use Automattic\WooCommerce\Client; |
12
|
|
|
use Automattic\WooCommerce\HttpClient\BasicAuth; |
13
|
|
|
use Automattic\WooCommerce\HttpClient\HttpClientException; |
14
|
|
|
use Automattic\WooCommerce\HttpClient\OAuth; |
15
|
|
|
use Automattic\WooCommerce\HttpClient\Options; |
16
|
|
|
use Automattic\WooCommerce\HttpClient\Request; |
17
|
|
|
use Automattic\WooCommerce\HttpClient\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* REST API HTTP Client class. |
21
|
|
|
* |
22
|
|
|
* @package Automattic/WooCommerce |
23
|
|
|
*/ |
24
|
|
|
class HttpClient |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* cURL handle. |
29
|
|
|
* |
30
|
|
|
* @var resource |
31
|
|
|
*/ |
32
|
|
|
protected $ch; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Store API URL. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $url; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Consumer key. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $consumerKey; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Consumer secret. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $consumerSecret; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Client options. |
57
|
|
|
* |
58
|
|
|
* @var Options |
59
|
|
|
*/ |
60
|
|
|
protected $options; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Request. |
64
|
|
|
* |
65
|
|
|
* @var Request |
66
|
|
|
*/ |
67
|
|
|
private $request; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Response. |
71
|
|
|
* |
72
|
|
|
* @var Response |
73
|
|
|
*/ |
74
|
|
|
private $response; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Response headers. |
78
|
|
|
* |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
private $responseHeaders; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Initialize HTTP client. |
85
|
|
|
* |
86
|
|
|
* @param string $url Store URL. |
87
|
|
|
* @param string $consumerKey Consumer key. |
88
|
|
|
* @param string $consumerSecret Consumer Secret. |
89
|
|
|
* @param array $options Client options. |
90
|
|
|
*/ |
91
|
|
|
public function __construct($url, $consumerKey, $consumerSecret, $options) |
92
|
|
|
{ |
93
|
|
|
if (!\function_exists('curl_version')) { |
94
|
|
|
throw new HttpClientException('cURL is NOT installed on this server', -1, new Request(), new Response()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->options = new Options($options); |
98
|
|
|
$this->url = $this->buildApiUrl($url); |
99
|
|
|
$this->consumerKey = $consumerKey; |
100
|
|
|
$this->consumerSecret = $consumerSecret; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Check if is under SSL. |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
protected function isSsl() |
109
|
|
|
{ |
110
|
|
|
return 'https://' === \substr($this->url, 0, 8); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Build API URL. |
115
|
|
|
* |
116
|
|
|
* @param string $url Store URL. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function buildApiUrl($url) |
121
|
|
|
{ |
122
|
|
|
$api = $this->options->isWPAPI() ? $this->options->apiPrefix() : '/wc-api/'; |
123
|
|
|
|
124
|
|
|
return \rtrim($url, '/') . $api . $this->options->getVersion() . '/'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Build URL. |
129
|
|
|
* |
130
|
|
|
* @param string $url URL. |
131
|
|
|
* @param array $parameters Query string parameters. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function buildUrlQuery($url, $parameters = []) |
136
|
|
|
{ |
137
|
|
|
if (!empty($parameters)) { |
138
|
|
|
$url .= '?' . \http_build_query($parameters); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $url; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Authenticate. |
146
|
|
|
* |
147
|
|
|
* @param string $url Request URL. |
148
|
|
|
* @param string $method Request method. |
149
|
|
|
* @param array $parameters Request parameters. |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
protected function authenticate($url, $method, $parameters = []) |
154
|
|
|
{ |
155
|
|
|
// Setup authentication. |
156
|
|
|
if ($this->isSsl()) { |
157
|
|
|
$basicAuth = new BasicAuth( |
158
|
|
|
$this->ch, |
159
|
|
|
$this->consumerKey, |
160
|
|
|
$this->consumerSecret, |
161
|
|
|
$this->options->isQueryStringAuth(), |
162
|
|
|
$parameters |
163
|
|
|
); |
164
|
|
|
$parameters = $basicAuth->getParameters(); |
165
|
|
|
} else { |
166
|
|
|
$oAuth = new OAuth( |
167
|
|
|
$url, |
168
|
|
|
$this->consumerKey, |
169
|
|
|
$this->consumerSecret, |
170
|
|
|
$this->options->getVersion(), |
171
|
|
|
$method, |
172
|
|
|
$parameters, |
173
|
|
|
$this->options->oauthTimestamp() |
174
|
|
|
); |
175
|
|
|
$parameters = $oAuth->getParameters(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $parameters; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Setup method. |
183
|
|
|
* |
184
|
|
|
* @param string $method Request method. |
185
|
|
|
*/ |
186
|
|
|
protected function setupMethod($method) |
187
|
|
|
{ |
188
|
|
|
if ('POST' == $method) { |
189
|
|
|
\curl_setopt($this->ch, CURLOPT_POST, true); |
190
|
|
|
} elseif (\in_array($method, ['PUT', 'DELETE', 'OPTIONS'])) { |
191
|
|
|
\curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get request headers. |
197
|
|
|
* |
198
|
|
|
* @param bool $sendData If request send data or not. |
199
|
|
|
* |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
protected function getRequestHeaders($sendData = false) |
203
|
|
|
{ |
204
|
|
|
$headers = [ |
205
|
|
|
'Accept' => 'application/json', |
206
|
|
|
'User-Agent' => $this->options->userAgent() . '/' . Client::VERSION, |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
if ($sendData) { |
210
|
|
|
$headers['Content-Type'] = 'application/json;charset=utf-8'; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $headers; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Create request. |
218
|
|
|
* |
219
|
|
|
* @param string $endpoint Request endpoint. |
220
|
|
|
* @param string $method Request method. |
221
|
|
|
* @param array $data Request data. |
222
|
|
|
* @param array $parameters Request parameters. |
223
|
|
|
* |
224
|
|
|
* @return Request |
225
|
|
|
*/ |
226
|
|
|
protected function createRequest($endpoint, $method, $data = [], $parameters = []) |
227
|
|
|
{ |
228
|
|
|
$body = ''; |
229
|
|
|
$url = $this->url . $endpoint; |
230
|
|
|
$hasData = !empty($data); |
231
|
|
|
|
232
|
|
|
// Setup authentication. |
233
|
|
|
$parameters = $this->authenticate($url, $method, $parameters); |
234
|
|
|
|
235
|
|
|
// Setup method. |
236
|
|
|
$this->setupMethod($method); |
237
|
|
|
|
238
|
|
|
// Include post fields. |
239
|
|
|
if ($hasData) { |
240
|
|
|
$body = \json_encode($data); |
241
|
|
|
\curl_setopt($this->ch, CURLOPT_POSTFIELDS, $body); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$this->request = new Request( |
245
|
|
|
$this->buildUrlQuery($url, $parameters), |
246
|
|
|
$method, |
247
|
|
|
$parameters, |
248
|
|
|
$this->getRequestHeaders($hasData), |
249
|
|
|
$body |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
return $this->getRequest(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get response headers. |
257
|
|
|
* |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
protected function getResponseHeaders() |
261
|
|
|
{ |
262
|
|
|
$headers = []; |
263
|
|
|
$lines = \explode("\n", $this->responseHeaders); |
264
|
|
|
$lines = \array_filter($lines, 'trim'); |
265
|
|
|
|
266
|
|
|
foreach ($lines as $index => $line) { |
267
|
|
|
// Remove HTTP/xxx params. |
268
|
|
|
if (strpos($line, ': ') === false) { |
269
|
|
|
continue; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
list($key, $value) = \explode(': ', $line); |
273
|
|
|
|
274
|
|
|
$headers[$key] = isset($headers[$key]) ? $headers[$key] . ', ' . trim($value) : trim($value); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $headers; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Create response. |
282
|
|
|
* |
283
|
|
|
* @return Response |
284
|
|
|
*/ |
285
|
|
|
protected function createResponse() |
286
|
|
|
{ |
287
|
|
|
|
288
|
|
|
// Set response headers. |
289
|
|
|
$this->responseHeaders = ''; |
290
|
|
|
\curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, function ($_, $headers) { |
291
|
|
|
$this->responseHeaders .= $headers; |
292
|
|
|
return \strlen($headers); |
293
|
|
|
}); |
294
|
|
|
|
295
|
|
|
// Get response data. |
296
|
|
|
$body = \curl_exec($this->ch); |
297
|
|
|
$code = \curl_getinfo($this->ch, CURLINFO_HTTP_CODE); |
298
|
|
|
$headers = $this->getResponseHeaders(); |
299
|
|
|
|
300
|
|
|
// Register response. |
301
|
|
|
$this->response = new Response($code, $headers, $body); |
302
|
|
|
|
303
|
|
|
return $this->getResponse(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Set default cURL settings. |
308
|
|
|
*/ |
309
|
|
|
protected function setDefaultCurlSettings() |
310
|
|
|
{ |
311
|
|
|
$verifySsl = $this->options->verifySsl(); |
312
|
|
|
$timeout = $this->options->getTimeout(); |
313
|
|
|
$followRedirects = $this->options->getFollowRedirects(); |
314
|
|
|
|
315
|
|
|
\curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, $verifySsl); |
316
|
|
|
if (!$verifySsl) { |
317
|
|
|
\curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, $verifySsl); |
318
|
|
|
} |
319
|
|
|
if ($followRedirects) { |
320
|
|
|
\curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); |
321
|
|
|
} |
322
|
|
|
\curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
323
|
|
|
\curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout); |
324
|
|
|
\curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); |
325
|
|
|
\curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->request->getRawHeaders()); |
326
|
|
|
\curl_setopt($this->ch, CURLOPT_URL, $this->request->getUrl()); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Look for errors in the request. |
331
|
|
|
* |
332
|
|
|
* @param array $parsedResponse Parsed body response. |
333
|
|
|
*/ |
334
|
|
|
protected function lookForErrors($parsedResponse) |
335
|
|
|
{ |
336
|
|
|
// Any non-200/201/202 response code indicates an error. |
337
|
|
|
if (!\in_array($this->response->getCode(), ['200', '201', '202'])) { |
338
|
|
|
$errors = isset($parsedResponse->errors) ? $parsedResponse->errors : $parsedResponse; |
339
|
|
|
|
340
|
|
|
if (is_array($errors)) { |
341
|
|
|
$errorMessage = $errors[0]['message']; |
342
|
|
|
$errorCode = $errors[0]['code']; |
343
|
|
|
} else { |
344
|
|
|
$errorMessage = $errors->message; |
345
|
|
|
$errorCode = $errors->code; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
throw new HttpClientException( |
349
|
|
|
\sprintf('Error: %s [%s]', $errorMessage, $errorCode), |
350
|
|
|
$this->response->getCode(), |
351
|
|
|
$this->request, |
352
|
|
|
$this->response |
353
|
|
|
); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Process response. |
359
|
|
|
* |
360
|
|
|
* @return array |
361
|
|
|
*/ |
362
|
|
|
protected function processResponse() |
363
|
|
|
{ |
364
|
|
|
$body = $this->response->getBody(); |
365
|
|
|
|
366
|
|
|
if (0 === strpos(bin2hex($body), 'efbbbf')) { |
367
|
|
|
$body = substr($body, 3); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$parsedResponse = \json_decode($body); |
371
|
|
|
|
372
|
|
|
// Test if return a valid JSON. |
373
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
374
|
|
|
$message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Invalid JSON returned'; |
375
|
|
|
throw new HttpClientException($message, $this->response->getCode(), $this->request, $this->response); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
$this->lookForErrors($parsedResponse); |
379
|
|
|
|
380
|
|
|
return $parsedResponse; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Make requests. |
385
|
|
|
* |
386
|
|
|
* @param string $endpoint Request endpoint. |
387
|
|
|
* @param string $method Request method. |
388
|
|
|
* @param array $data Request data. |
389
|
|
|
* @param array $parameters Request parameters. |
390
|
|
|
* |
391
|
|
|
* @return array |
392
|
|
|
*/ |
393
|
|
|
public function request($endpoint, $method, $data = [], $parameters = []) |
394
|
|
|
{ |
395
|
|
|
// Initialize cURL. |
396
|
|
|
$this->ch = \curl_init(); |
397
|
|
|
|
398
|
|
|
// Set request args. |
399
|
|
|
$request = $this->createRequest($endpoint, $method, $data, $parameters); |
400
|
|
|
|
401
|
|
|
// Default cURL settings. |
402
|
|
|
$this->setDefaultCurlSettings(); |
403
|
|
|
|
404
|
|
|
// Get response. |
405
|
|
|
$response = $this->createResponse(); |
406
|
|
|
|
407
|
|
|
// Check for cURL errors. |
408
|
|
|
if (\curl_errno($this->ch)) { |
409
|
|
|
throw new HttpClientException('cURL Error: ' . \curl_error($this->ch), 0, $request, $response); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
\curl_close($this->ch); |
413
|
|
|
|
414
|
|
|
return $this->processResponse(); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Get request data. |
419
|
|
|
* |
420
|
|
|
* @return Request |
421
|
|
|
*/ |
422
|
|
|
public function getRequest() |
423
|
|
|
{ |
424
|
|
|
return $this->request; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Get response data. |
429
|
|
|
* |
430
|
|
|
* @return Response |
431
|
|
|
*/ |
432
|
|
|
public function getResponse() |
433
|
|
|
{ |
434
|
|
|
return $this->response; |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|