-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathclass-wc-api-client-http-request.php
More file actions
372 lines (282 loc) · 9.93 KB
/
class-wc-api-client-http-request.php
File metadata and controls
372 lines (282 loc) · 9.93 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* WC API Client HTTP Request
*
* Handles HTTP communication to the WC API via cURL
*
* @since 2.0
*/
class WC_API_Client_HTTP_Request {
/** @var resource cURL handle */
protected $ch;
/** @var stdClass request data */
protected $request;
/** @var bool true to decode JSON as array */
protected $json_decode_as_array;
/** @var bool true to include cURL log, HTTP request/response object in result */
protected $debug;
/** @var stdClass response data */
protected $response;
/** @var string raw HTTP response headers */
protected $curl_headers;
/**
* Setup HTTP request
*
* @since 2.0
* @param array $args request args
*/
public function __construct( $args ) {
$this->request = new stdClass();
$this->request->headers = array(
'Accept: application/json',
'Content-Type: application/json',
'User-Agent: WooCommerce API Client-PHP/' . WC_API_Client::VERSION,
);
// GET, POST, PUT, DELETE, etc.
$this->request->method = $args['method'];
// trailing slashes tend to cause OAuth authentication issues, so strip them
$this->request->url = rtrim( $args['url'], '/' );
$this->request->params = array();
$this->request->data = $args['data'];
// JSON output format
$this->json_decode_as_array = ( 'array' === $args['options']['json_decode'] );
// debug mode?
$this->debug = (bool) $args['options']['debug'];
// optional cURL opts
$timeout = (int) $args['options']['timeout'];
$ssl_verify = (bool) $args['options']['ssl_verify'];
$this->ch = curl_init();
// default cURL opts
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, $ssl_verify );
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, $ssl_verify );
curl_setopt( $this->ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $this->ch, CURLOPT_TIMEOUT, (int) $timeout );
curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, true );
// set request headers
curl_setopt( $this->ch, CURLOPT_HTTPHEADER, $this->request->headers );
// save response headers
curl_setopt( $this->ch, CURLOPT_HEADERFUNCTION, array( $this, 'curl_stream_headers' ) );
// set request method and data
switch ( $this->request->method ) {
case 'GET':
$this->request->body = null;
$this->request->params = (array) $this->request->data;
break;
case 'PUT':
$this->request->body = json_encode( $this->request->data );
curl_setopt( $this->ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $this->request->body );
break;
case 'POST':
$this->request->body = json_encode( $this->request->data );
curl_setopt( $this->ch, CURLOPT_POST, true );
curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $this->request->body );
break;
case 'DELETE':
$this->request->body = null;
$this->request->params = (array) $this->request->data;
curl_setopt( $this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
break;
}
// auth
$this->request->url = $this->get_url_with_auth( $args['consumer_key'], $args['consumer_secret'] );
// set request url
curl_setopt( $this->ch, CURLOPT_URL, $this->request->url );
}
/**
* Set authentication parameters for the request
*
* @since 2.0
* @param string $consumer_key API consumer key
* @param string $consumer_secret API consumer secret
* @return string request URL with authentication parameters added
*/
protected function get_url_with_auth( $consumer_key, $consumer_secret ) {
$auth = new WC_API_Client_Authentication( $this->request->url, $consumer_key, $consumer_secret );
if ( $auth->is_ssl() ) {
// query string authentication over SSL works with all servers, whereas HTTP basic auth fails in certain cases
// see https://github.com/kloon/WooCommerce-REST-API-Client-Library/issues/29
$this->request->params = array_merge( $this->request->params, array(
'consumer_key' => $auth->get_consumer_key(),
'consumer_secret' => $auth->get_consumer_secret(),
) );
} else {
$this->request->params = array_merge( $this->request->params, $auth->get_oauth_params( $this->request->params, $this->request->method ) );
}
// build url
return $this->request->url . ( ! empty( $this->request->params ) ? '?' . http_build_query( $this->request->params ) : '' );
}
/**
* Send the request
*
* @since 2.0
* @return object|array result
* @throws WC_API_Client_HTTP_Exception invalid decoded JSON or any non-HTTP 200/201 response
*/
public function dispatch() {
$this->response = new stdClass();
// blank headers
$this->curl_headers = '';
$start_time = microtime( true );
// send request + save raw response body
$this->response->body = curl_exec( $this->ch );
// request duration
$this->request->duration = round( microtime( true ) - $start_time, 5 );
// response code
$this->response->code = curl_getinfo( $this->ch, CURLINFO_HTTP_CODE );
// response headers
$this->response->headers = $this->get_response_headers();
curl_close( $this->ch );
$parsed_response = $this->get_parsed_response( $this->response->body );
// check for invalid JSON
if ( null === $parsed_response ) {
throw new WC_API_Client_HTTP_Exception( sprintf( 'Invalid JSON returned for %s.', $this->request->url ), $this->response->code, $this->request, $this->response );
}
// any non-200/201/202 response code indicates an error
if ( ! in_array( $this->response->code, array( '200', '201', '202' ) ) ) {
// error message/code is nested sometimes
if ( $this->json_decode_as_array ) {
list( $error_message, $error_code ) = is_array( $parsed_response['errors'] ) ? array(
$parsed_response['errors'][0]['message'],
$parsed_response['errors'][0]['code']
) : array(
$parsed_response['errors']['message'],
$parsed_response['errors']['code']
);
} else {
list( $error_message, $error_code ) = is_array( $parsed_response->errors ) ? array(
$parsed_response->errors[0]->message,
$parsed_response->errors[0]->code
) : array(
$parsed_response->errors->message,
$parsed_response->errors->code
);
}
throw new WC_API_Client_HTTP_Exception( sprintf( 'Error: %s [%s]', $error_message, $error_code ), $this->response->code, $this->request, $this->response);
}
return $this->build_result( $parsed_response );
}
/**
* JSON decode the response body after stripping any invalid leading or
* trailing characters.
*
* Plugins (looking at you WP Super Cache) or themes
* can add output to the returned JSON which breaks decoding.
*
* @since 2.0
* @param string $raw_body raw response body
* @return object|array JSON decoded response body
*/
protected function get_parsed_response( $raw_body ) {
$json_start = strpos( $raw_body, '{' );
$json_end = strrpos( $raw_body, '}' ) + 1; // inclusive
$json = substr( $raw_body, $json_start, ( $json_end - $json_start ) );
$array = json_decode( $json, $this->json_decode_as_array );
return $this->get_extended_response($array);
}
/**
* Adds the response headers to the response
*
* Useful for pagination since the total number of resources and pages are always included
* in the X-WC-Total and X-WC-TotalPages HTTP headers.
*
* @param object|array $response
* @return mixed
*/
protected function get_extended_response( $response ) {
$response->header =$this->response->headers;
return $response;
}
/**
* Build the result object/array
*
* @since 2.0.0
* @param object|array JSON decoded result
* @return object|array in format:
* {
* <result data>
* 'http' =>
* 'request' => stdClass(
* 'url' => request URL
* 'method' => request method
* 'body' => JSON encoded request body entity
* 'headers' => array of request headers
* 'duration' => request duration, in seconds
* 'params' => optional raw params
* 'data' => optional raw request data
* 'duration' =>
* )
* 'response' => stdClass(
* 'body' => raw response body
* 'code' => HTTP response code
* 'headers' => HTTP response headers in assoc array
* )
* }
*/
protected function build_result( $parsed_response ) {
// add cURL log, HTTP request/response object
if ( $this->debug ) {
if ( $this->json_decode_as_array ) {
$parsed_response['http'] = array(
'request' => json_decode( json_encode( $this->request ), true ),
'response' => json_decode( json_encode( $this->response ), true ),
);
} else {
$parsed_response->http = new stdClass();
$parsed_response->http->request = $this->request;
$parsed_response->http->response = $this->response;
}
}
return $parsed_response;
}
/**
* Save the cURL response headers for later processing
*
* @since 2.0
* @see WP_Http_Curl::stream_headers()
* @param object $_ the cURL resource handle (unused)
* @param string $headers the current response headers
* @return int the size of the processed headers
*/
public function curl_stream_headers( $_, $headers ) {
$this->curl_headers .= $headers;
return strlen( $headers );
}
/**
* Parse the raw response headers into an assoc array in format:
* {
* 'Header-Key' => header value
* 'Duplicate-Key' => array(
* 0 => value 1
* 1 => value 2
* )
* }
*
* @since 2.0
* @see WP_HTTP::processHeaders
* @return array
*/
protected function get_response_headers() {
// get the raw headers
$raw_headers = preg_replace('/\n[ \t]/', ' ', str_replace( "\r\n", "\n", $this->curl_headers ) );
// spit them
$raw_headers = array_filter( explode( "\n", $raw_headers ), 'strlen' );
$headers = array();
// parse into assoc array
foreach ( $raw_headers as $header ) {
// skip response codes (appears as HTTP/1.1 200 OK or HTTP/1.1 100 Continue)
if ( 'HTTP/' === substr( $header, 0, 5 ) ) {
continue;
}
list( $key, $value ) = explode( ':', $header, 2 );
if ( isset( $headers[ $key ] ) ) {
// ensure duplicate headers aren't overwritten
$headers[ $key ] = array( $headers[ $key ] );
$headers[ $key ][] = $value;
} else {
$headers[ $key ] = $value;
}
}
return $headers;
}
}