| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Handles all HTTP requests using cURL and manages the responses.
|
|
|
4 |
*
|
|
|
5 |
* @version 2012.01.17
|
|
|
6 |
* @copyright 2006-2011 Ryan Parman
|
|
|
7 |
* @copyright 2006-2010 Foleeo Inc.
|
|
|
8 |
* @copyright 2010-2011 Amazon.com, Inc. or its affiliates.
|
|
|
9 |
* @copyright 2008-2011 Contributors
|
|
|
10 |
* @license http://opensource.org/licenses/bsd-license.php Simplified BSD License
|
|
|
11 |
*/
|
|
|
12 |
class RequestCore
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* The URL being requested.
|
|
|
16 |
*/
|
|
|
17 |
public $request_url;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* The headers being sent in the request.
|
|
|
21 |
*/
|
|
|
22 |
public $request_headers;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* The body being sent in the request.
|
|
|
26 |
*/
|
|
|
27 |
public $request_body;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* The response returned by the request.
|
|
|
31 |
*/
|
|
|
32 |
public $response;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* The headers returned by the request.
|
|
|
36 |
*/
|
|
|
37 |
public $response_headers;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* The body returned by the request.
|
|
|
41 |
*/
|
|
|
42 |
public $response_body;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* The HTTP status code returned by the request.
|
|
|
46 |
*/
|
|
|
47 |
public $response_code;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Additional response data.
|
|
|
51 |
*/
|
|
|
52 |
public $response_info;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* The handle for the cURL object.
|
|
|
56 |
*/
|
|
|
57 |
public $curl_handle;
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* The method by which the request is being made.
|
|
|
61 |
*/
|
|
|
62 |
public $method;
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Stores the proxy settings to use for the request.
|
|
|
66 |
*/
|
|
|
67 |
public $proxy = null;
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* The username to use for the request.
|
|
|
71 |
*/
|
|
|
72 |
public $username = null;
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* The password to use for the request.
|
|
|
76 |
*/
|
|
|
77 |
public $password = null;
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Custom CURLOPT settings.
|
|
|
81 |
*/
|
|
|
82 |
public $curlopts = null;
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* The state of debug mode.
|
|
|
86 |
*/
|
|
|
87 |
public $debug_mode = false;
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* The default class to use for HTTP Requests (defaults to <RequestCore>).
|
|
|
91 |
*/
|
|
|
92 |
public $request_class = 'RequestCore';
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* The default class to use for HTTP Responses (defaults to <ResponseCore>).
|
|
|
96 |
*/
|
|
|
97 |
public $response_class = 'ResponseCore';
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Default useragent string to use.
|
|
|
101 |
*/
|
|
|
102 |
public $useragent = 'RequestCore/1.4.4';
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* File to read from while streaming up.
|
|
|
106 |
*/
|
|
|
107 |
public $read_file = null;
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* The resource to read from while streaming up.
|
|
|
111 |
*/
|
|
|
112 |
public $read_stream = null;
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* The size of the stream to read from.
|
|
|
116 |
*/
|
|
|
117 |
public $read_stream_size = null;
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* The length already read from the stream.
|
|
|
121 |
*/
|
|
|
122 |
public $read_stream_read = 0;
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* File to write to while streaming down.
|
|
|
126 |
*/
|
|
|
127 |
public $write_file = null;
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* The resource to write to while streaming down.
|
|
|
131 |
*/
|
|
|
132 |
public $write_stream = null;
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Stores the intended starting seek position.
|
|
|
136 |
*/
|
|
|
137 |
public $seek_position = null;
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* The location of the cacert.pem file to use.
|
|
|
141 |
*/
|
|
|
142 |
public $cacert_location = false;
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* The state of SSL certificate verification.
|
|
|
146 |
*/
|
|
|
147 |
public $ssl_verification = true;
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* The user-defined callback function to call when a stream is read from.
|
|
|
151 |
*/
|
|
|
152 |
public $registered_streaming_read_callback = null;
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* The user-defined callback function to call when a stream is written to.
|
|
|
156 |
*/
|
|
|
157 |
public $registered_streaming_write_callback = null;
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
/*%******************************************************************************************%*/
|
|
|
161 |
// CONSTANTS
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* GET HTTP Method
|
|
|
165 |
*/
|
|
|
166 |
const HTTP_GET = 'GET';
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* POST HTTP Method
|
|
|
170 |
*/
|
|
|
171 |
const HTTP_POST = 'POST';
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* PUT HTTP Method
|
|
|
175 |
*/
|
|
|
176 |
const HTTP_PUT = 'PUT';
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* DELETE HTTP Method
|
|
|
180 |
*/
|
|
|
181 |
const HTTP_DELETE = 'DELETE';
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* HEAD HTTP Method
|
|
|
185 |
*/
|
|
|
186 |
const HTTP_HEAD = 'HEAD';
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
/*%******************************************************************************************%*/
|
|
|
190 |
// CONSTRUCTOR/DESTRUCTOR
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* Constructs a new instance of this class.
|
|
|
194 |
*
|
|
|
195 |
* @param string $url (Optional) The URL to request or service endpoint to query.
|
|
|
196 |
* @param string $proxy (Optional) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port`
|
|
|
197 |
* @param array $helpers (Optional) An associative array of classnames to use for request, and response functionality. Gets passed in automatically by the calling class.
|
|
|
198 |
* @return $this A reference to the current instance.
|
|
|
199 |
*/
|
|
|
200 |
public function __construct($url = null, $proxy = null, $helpers = null)
|
|
|
201 |
{
|
|
|
202 |
// Set some default values.
|
|
|
203 |
$this->request_url = $url;
|
|
|
204 |
$this->method = self::HTTP_GET;
|
|
|
205 |
$this->request_headers = array();
|
|
|
206 |
$this->request_body = '';
|
|
|
207 |
|
|
|
208 |
// Set a new Request class if one was set.
|
|
|
209 |
if (isset($helpers['request']) && !empty($helpers['request']))
|
|
|
210 |
{
|
|
|
211 |
$this->request_class = $helpers['request'];
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
// Set a new Request class if one was set.
|
|
|
215 |
if (isset($helpers['response']) && !empty($helpers['response']))
|
|
|
216 |
{
|
|
|
217 |
$this->response_class = $helpers['response'];
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
if ($proxy)
|
|
|
221 |
{
|
|
|
222 |
$this->set_proxy($proxy);
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
return $this;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
* Destructs the instance. Closes opened file handles.
|
|
|
230 |
*
|
|
|
231 |
* @return $this A reference to the current instance.
|
|
|
232 |
*/
|
|
|
233 |
public function __destruct()
|
|
|
234 |
{
|
|
|
235 |
if (isset($this->read_file) && isset($this->read_stream))
|
|
|
236 |
{
|
|
|
237 |
fclose($this->read_stream);
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
if (isset($this->write_file) && isset($this->write_stream))
|
|
|
241 |
{
|
|
|
242 |
fclose($this->write_stream);
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
return $this;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
|
|
|
249 |
/*%******************************************************************************************%*/
|
|
|
250 |
// REQUEST METHODS
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
* Sets the credentials to use for authentication.
|
|
|
254 |
*
|
|
|
255 |
* @param string $user (Required) The username to authenticate with.
|
|
|
256 |
* @param string $pass (Required) The password to authenticate with.
|
|
|
257 |
* @return $this A reference to the current instance.
|
|
|
258 |
*/
|
|
|
259 |
public function set_credentials($user, $pass)
|
|
|
260 |
{
|
|
|
261 |
$this->username = $user;
|
|
|
262 |
$this->password = $pass;
|
|
|
263 |
return $this;
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* Adds a custom HTTP header to the cURL request.
|
|
|
268 |
*
|
|
|
269 |
* @param string $key (Required) The custom HTTP header to set.
|
|
|
270 |
* @param mixed $value (Required) The value to assign to the custom HTTP header.
|
|
|
271 |
* @return $this A reference to the current instance.
|
|
|
272 |
*/
|
|
|
273 |
public function add_header($key, $value)
|
|
|
274 |
{
|
|
|
275 |
$this->request_headers[$key] = $value;
|
|
|
276 |
return $this;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Removes an HTTP header from the cURL request.
|
|
|
281 |
*
|
|
|
282 |
* @param string $key (Required) The custom HTTP header to set.
|
|
|
283 |
* @return $this A reference to the current instance.
|
|
|
284 |
*/
|
|
|
285 |
public function remove_header($key)
|
|
|
286 |
{
|
|
|
287 |
if (isset($this->request_headers[$key]))
|
|
|
288 |
{
|
|
|
289 |
unset($this->request_headers[$key]);
|
|
|
290 |
}
|
|
|
291 |
return $this;
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
* Set the method type for the request.
|
|
|
296 |
*
|
|
|
297 |
* @param string $method (Required) One of the following constants: <HTTP_GET>, <HTTP_POST>, <HTTP_PUT>, <HTTP_HEAD>, <HTTP_DELETE>.
|
|
|
298 |
* @return $this A reference to the current instance.
|
|
|
299 |
*/
|
|
|
300 |
public function set_method($method)
|
|
|
301 |
{
|
|
|
302 |
$this->method = strtoupper($method);
|
|
|
303 |
return $this;
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
/**
|
|
|
307 |
* Sets a custom useragent string for the class.
|
|
|
308 |
*
|
|
|
309 |
* @param string $ua (Required) The useragent string to use.
|
|
|
310 |
* @return $this A reference to the current instance.
|
|
|
311 |
*/
|
|
|
312 |
public function set_useragent($ua)
|
|
|
313 |
{
|
|
|
314 |
$this->useragent = $ua;
|
|
|
315 |
return $this;
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* Set the body to send in the request.
|
|
|
320 |
*
|
|
|
321 |
* @param string $body (Required) The textual content to send along in the body of the request.
|
|
|
322 |
* @return $this A reference to the current instance.
|
|
|
323 |
*/
|
|
|
324 |
public function set_body($body)
|
|
|
325 |
{
|
|
|
326 |
$this->request_body = $body;
|
|
|
327 |
return $this;
|
|
|
328 |
}
|
|
|
329 |
|
|
|
330 |
/**
|
|
|
331 |
* Set the URL to make the request to.
|
|
|
332 |
*
|
|
|
333 |
* @param string $url (Required) The URL to make the request to.
|
|
|
334 |
* @return $this A reference to the current instance.
|
|
|
335 |
*/
|
|
|
336 |
public function set_request_url($url)
|
|
|
337 |
{
|
|
|
338 |
$this->request_url = $url;
|
|
|
339 |
return $this;
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
/**
|
|
|
343 |
* Set additional CURLOPT settings. These will merge with the default settings, and override if
|
|
|
344 |
* there is a duplicate.
|
|
|
345 |
*
|
|
|
346 |
* @param array $curlopts (Optional) A set of key-value pairs that set `CURLOPT` options. These will merge with the existing CURLOPTs, and ones passed here will override the defaults. Keys should be the `CURLOPT_*` constants, not strings.
|
|
|
347 |
* @return $this A reference to the current instance.
|
|
|
348 |
*/
|
|
|
349 |
public function set_curlopts($curlopts)
|
|
|
350 |
{
|
|
|
351 |
$this->curlopts = $curlopts;
|
|
|
352 |
return $this;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
/**
|
|
|
356 |
* Sets the length in bytes to read from the stream while streaming up.
|
|
|
357 |
*
|
|
|
358 |
* @param integer $size (Required) The length in bytes to read from the stream.
|
|
|
359 |
* @return $this A reference to the current instance.
|
|
|
360 |
*/
|
|
|
361 |
public function set_read_stream_size($size)
|
|
|
362 |
{
|
|
|
363 |
$this->read_stream_size = $size;
|
|
|
364 |
|
|
|
365 |
return $this;
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
/**
|
|
|
369 |
* Sets the resource to read from while streaming up. Reads the stream from its current position until
|
|
|
370 |
* EOF or `$size` bytes have been read. If `$size` is not given it will be determined by <php:fstat()> and
|
|
|
371 |
* <php:ftell()>.
|
|
|
372 |
*
|
|
|
373 |
* @param resource $resource (Required) The readable resource to read from.
|
|
|
374 |
* @param integer $size (Optional) The size of the stream to read.
|
|
|
375 |
* @return $this A reference to the current instance.
|
|
|
376 |
*/
|
|
|
377 |
public function set_read_stream($resource, $size = null)
|
|
|
378 |
{
|
|
|
379 |
if (!isset($size) || $size < 0)
|
|
|
380 |
{
|
|
|
381 |
$stats = fstat($resource);
|
|
|
382 |
|
|
|
383 |
if ($stats && $stats['size'] >= 0)
|
|
|
384 |
{
|
|
|
385 |
$position = ftell($resource);
|
|
|
386 |
|
|
|
387 |
if ($position !== false && $position >= 0)
|
|
|
388 |
{
|
|
|
389 |
$size = $stats['size'] - $position;
|
|
|
390 |
}
|
|
|
391 |
}
|
|
|
392 |
}
|
|
|
393 |
|
|
|
394 |
$this->read_stream = $resource;
|
|
|
395 |
|
|
|
396 |
return $this->set_read_stream_size($size);
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
/**
|
|
|
400 |
* Sets the file to read from while streaming up.
|
|
|
401 |
*
|
|
|
402 |
* @param string $location (Required) The readable location to read from.
|
|
|
403 |
* @return $this A reference to the current instance.
|
|
|
404 |
*/
|
|
|
405 |
public function set_read_file($location)
|
|
|
406 |
{
|
|
|
407 |
$this->read_file = $location;
|
|
|
408 |
$read_file_handle = fopen($location, 'r');
|
|
|
409 |
|
|
|
410 |
return $this->set_read_stream($read_file_handle);
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
/**
|
|
|
414 |
* Sets the resource to write to while streaming down.
|
|
|
415 |
*
|
|
|
416 |
* @param resource $resource (Required) The writeable resource to write to.
|
|
|
417 |
* @return $this A reference to the current instance.
|
|
|
418 |
*/
|
|
|
419 |
public function set_write_stream($resource)
|
|
|
420 |
{
|
|
|
421 |
$this->write_stream = $resource;
|
|
|
422 |
|
|
|
423 |
return $this;
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
/**
|
|
|
427 |
* Sets the file to write to while streaming down.
|
|
|
428 |
*
|
|
|
429 |
* @param string $location (Required) The writeable location to write to.
|
|
|
430 |
* @return $this A reference to the current instance.
|
|
|
431 |
*/
|
|
|
432 |
public function set_write_file($location)
|
|
|
433 |
{
|
|
|
434 |
$this->write_file = $location;
|
|
|
435 |
$write_file_handle = fopen($location, 'w');
|
|
|
436 |
|
|
|
437 |
return $this->set_write_stream($write_file_handle);
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
/**
|
|
|
441 |
* Set the proxy to use for making requests.
|
|
|
442 |
*
|
|
|
443 |
* @param string $proxy (Required) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port`
|
|
|
444 |
* @return $this A reference to the current instance.
|
|
|
445 |
*/
|
|
|
446 |
public function set_proxy($proxy)
|
|
|
447 |
{
|
|
|
448 |
$proxy = parse_url($proxy);
|
|
|
449 |
$proxy['user'] = isset($proxy['user']) ? $proxy['user'] : null;
|
|
|
450 |
$proxy['pass'] = isset($proxy['pass']) ? $proxy['pass'] : null;
|
|
|
451 |
$proxy['port'] = isset($proxy['port']) ? $proxy['port'] : null;
|
|
|
452 |
$this->proxy = $proxy;
|
|
|
453 |
return $this;
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
/**
|
|
|
457 |
* Set the intended starting seek position.
|
|
|
458 |
*
|
|
|
459 |
* @param integer $position (Required) The byte-position of the stream to begin reading from.
|
|
|
460 |
* @return $this A reference to the current instance.
|
|
|
461 |
*/
|
|
|
462 |
public function set_seek_position($position)
|
|
|
463 |
{
|
|
|
464 |
$this->seek_position = isset($position) ? (integer) $position : null;
|
|
|
465 |
|
|
|
466 |
return $this;
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
/**
|
|
|
470 |
* Register a callback function to execute whenever a data stream is read from using
|
|
|
471 |
* <CFRequest::streaming_read_callback()>.
|
|
|
472 |
*
|
|
|
473 |
* The user-defined callback function should accept three arguments:
|
|
|
474 |
*
|
|
|
475 |
* <ul>
|
|
|
476 |
* <li><code>$curl_handle</code> - <code>resource</code> - Required - The cURL handle resource that represents the in-progress transfer.</li>
|
|
|
477 |
* <li><code>$file_handle</code> - <code>resource</code> - Required - The file handle resource that represents the file on the local file system.</li>
|
|
|
478 |
* <li><code>$length</code> - <code>integer</code> - Required - The length in kilobytes of the data chunk that was transferred.</li>
|
|
|
479 |
* </ul>
|
|
|
480 |
*
|
|
|
481 |
* @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul>
|
|
|
482 |
* <li>The name of a global function to execute, passed as a string.</li>
|
|
|
483 |
* <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li>
|
|
|
484 |
* <li>An anonymous function (PHP 5.3+).</li></ul>
|
|
|
485 |
* @return $this A reference to the current instance.
|
|
|
486 |
*/
|
|
|
487 |
public function register_streaming_read_callback($callback)
|
|
|
488 |
{
|
|
|
489 |
$this->registered_streaming_read_callback = $callback;
|
|
|
490 |
|
|
|
491 |
return $this;
|
|
|
492 |
}
|
|
|
493 |
|
|
|
494 |
/**
|
|
|
495 |
* Register a callback function to execute whenever a data stream is written to using
|
|
|
496 |
* <CFRequest::streaming_write_callback()>.
|
|
|
497 |
*
|
|
|
498 |
* The user-defined callback function should accept two arguments:
|
|
|
499 |
*
|
|
|
500 |
* <ul>
|
|
|
501 |
* <li><code>$curl_handle</code> - <code>resource</code> - Required - The cURL handle resource that represents the in-progress transfer.</li>
|
|
|
502 |
* <li><code>$length</code> - <code>integer</code> - Required - The length in kilobytes of the data chunk that was transferred.</li>
|
|
|
503 |
* </ul>
|
|
|
504 |
*
|
|
|
505 |
* @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul>
|
|
|
506 |
* <li>The name of a global function to execute, passed as a string.</li>
|
|
|
507 |
* <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li>
|
|
|
508 |
* <li>An anonymous function (PHP 5.3+).</li></ul>
|
|
|
509 |
* @return $this A reference to the current instance.
|
|
|
510 |
*/
|
|
|
511 |
public function register_streaming_write_callback($callback)
|
|
|
512 |
{
|
|
|
513 |
$this->registered_streaming_write_callback = $callback;
|
|
|
514 |
|
|
|
515 |
return $this;
|
|
|
516 |
}
|
|
|
517 |
|
|
|
518 |
|
|
|
519 |
/*%******************************************************************************************%*/
|
|
|
520 |
// PREPARE, SEND, AND PROCESS REQUEST
|
|
|
521 |
|
|
|
522 |
/**
|
|
|
523 |
* A callback function that is invoked by cURL for streaming up.
|
|
|
524 |
*
|
|
|
525 |
* @param resource $curl_handle (Required) The cURL handle for the request.
|
|
|
526 |
* @param resource $file_handle (Required) The open file handle resource.
|
|
|
527 |
* @param integer $length (Required) The maximum number of bytes to read.
|
|
|
528 |
* @return binary Binary data from a stream.
|
|
|
529 |
*/
|
|
|
530 |
public function streaming_read_callback($curl_handle, $file_handle, $length)
|
|
|
531 |
{
|
|
|
532 |
// Once we've sent as much as we're supposed to send...
|
|
|
533 |
if ($this->read_stream_read >= $this->read_stream_size)
|
|
|
534 |
{
|
|
|
535 |
// Send EOF
|
|
|
536 |
return '';
|
|
|
537 |
}
|
|
|
538 |
|
|
|
539 |
// If we're at the beginning of an upload and need to seek...
|
|
|
540 |
if ($this->read_stream_read == 0 && isset($this->seek_position) && $this->seek_position !== ftell($this->read_stream))
|
|
|
541 |
{
|
|
|
542 |
if (fseek($this->read_stream, $this->seek_position) !== 0)
|
|
|
543 |
{
|
|
|
544 |
throw new RequestCore_Exception('The stream does not support seeking and is either not at the requested position or the position is unknown.');
|
|
|
545 |
}
|
|
|
546 |
}
|
|
|
547 |
|
|
|
548 |
$read = fread($this->read_stream, min($this->read_stream_size - $this->read_stream_read, $length)); // Remaining upload data or cURL's requested chunk size
|
|
|
549 |
$this->read_stream_read += strlen($read);
|
|
|
550 |
|
|
|
551 |
$out = $read === false ? '' : $read;
|
|
|
552 |
|
|
|
553 |
// Execute callback function
|
|
|
554 |
if ($this->registered_streaming_read_callback)
|
|
|
555 |
{
|
|
|
556 |
call_user_func($this->registered_streaming_read_callback, $curl_handle, $file_handle, $out);
|
|
|
557 |
}
|
|
|
558 |
|
|
|
559 |
return $out;
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
/**
|
|
|
563 |
* A callback function that is invoked by cURL for streaming down.
|
|
|
564 |
*
|
|
|
565 |
* @param resource $curl_handle (Required) The cURL handle for the request.
|
|
|
566 |
* @param binary $data (Required) The data to write.
|
|
|
567 |
* @return integer The number of bytes written.
|
|
|
568 |
*/
|
|
|
569 |
public function streaming_write_callback($curl_handle, $data)
|
|
|
570 |
{
|
|
|
571 |
$length = strlen($data);
|
|
|
572 |
$written_total = 0;
|
|
|
573 |
$written_last = 0;
|
|
|
574 |
|
|
|
575 |
while ($written_total < $length)
|
|
|
576 |
{
|
|
|
577 |
$written_last = fwrite($this->write_stream, substr($data, $written_total));
|
|
|
578 |
|
|
|
579 |
if ($written_last === false)
|
|
|
580 |
{
|
|
|
581 |
return $written_total;
|
|
|
582 |
}
|
|
|
583 |
|
|
|
584 |
$written_total += $written_last;
|
|
|
585 |
}
|
|
|
586 |
|
|
|
587 |
// Execute callback function
|
|
|
588 |
if ($this->registered_streaming_write_callback)
|
|
|
589 |
{
|
|
|
590 |
call_user_func($this->registered_streaming_write_callback, $curl_handle, $written_total);
|
|
|
591 |
}
|
|
|
592 |
|
|
|
593 |
return $written_total;
|
|
|
594 |
}
|
|
|
595 |
|
|
|
596 |
/**
|
|
|
597 |
* Prepares and adds the details of the cURL request. This can be passed along to a <php:curl_multi_exec()>
|
|
|
598 |
* function.
|
|
|
599 |
*
|
|
|
600 |
* @return resource The handle for the cURL object.
|
|
|
601 |
*/
|
|
|
602 |
public function prep_request()
|
|
|
603 |
{
|
|
|
604 |
$curl_handle = curl_init();
|
|
|
605 |
|
|
|
606 |
// Set default options.
|
|
|
607 |
curl_setopt($curl_handle, CURLOPT_URL, $this->request_url);
|
|
|
608 |
curl_setopt($curl_handle, CURLOPT_FILETIME, true);
|
|
|
609 |
curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, false);
|
|
|
610 |
curl_setopt($curl_handle, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
|
|
|
611 |
curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5);
|
|
|
612 |
curl_setopt($curl_handle, CURLOPT_HEADER, true);
|
|
|
613 |
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
|
|
|
614 |
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5184000);
|
|
|
615 |
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 120);
|
|
|
616 |
curl_setopt($curl_handle, CURLOPT_NOSIGNAL, true);
|
|
|
617 |
curl_setopt($curl_handle, CURLOPT_REFERER, $this->request_url);
|
|
|
618 |
curl_setopt($curl_handle, CURLOPT_USERAGENT, $this->useragent);
|
|
|
619 |
curl_setopt($curl_handle, CURLOPT_READFUNCTION, array($this, 'streaming_read_callback'));
|
|
|
620 |
|
|
|
621 |
// Verification of the SSL cert
|
|
|
622 |
if ($this->ssl_verification)
|
|
|
623 |
{
|
|
|
624 |
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
625 |
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, true);
|
|
|
626 |
}
|
|
|
627 |
else
|
|
|
628 |
{
|
|
|
629 |
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
630 |
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
631 |
}
|
|
|
632 |
|
|
|
633 |
// chmod the file as 0755
|
|
|
634 |
if ($this->cacert_location === true)
|
|
|
635 |
{
|
|
|
636 |
curl_setopt($curl_handle, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
|
|
|
637 |
}
|
|
|
638 |
elseif (is_string($this->cacert_location))
|
|
|
639 |
{
|
|
|
640 |
curl_setopt($curl_handle, CURLOPT_CAINFO, $this->cacert_location);
|
|
|
641 |
}
|
|
|
642 |
|
|
|
643 |
// Debug mode
|
|
|
644 |
if ($this->debug_mode)
|
|
|
645 |
{
|
|
|
646 |
curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
|
|
|
647 |
}
|
|
|
648 |
|
|
|
649 |
// Handle open_basedir & safe mode
|
|
|
650 |
if (!ini_get('safe_mode') && !ini_get('open_basedir'))
|
|
|
651 |
{
|
|
|
652 |
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
|
|
|
653 |
}
|
|
|
654 |
|
|
|
655 |
// Enable a proxy connection if requested.
|
|
|
656 |
if ($this->proxy)
|
|
|
657 |
{
|
|
|
658 |
curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, true);
|
|
|
659 |
|
|
|
660 |
$host = $this->proxy['host'];
|
|
|
661 |
$host .= ($this->proxy['port']) ? ':' . $this->proxy['port'] : '';
|
|
|
662 |
curl_setopt($curl_handle, CURLOPT_PROXY, $host);
|
|
|
663 |
|
|
|
664 |
if (isset($this->proxy['user']) && isset($this->proxy['pass']))
|
|
|
665 |
{
|
|
|
666 |
curl_setopt($curl_handle, CURLOPT_PROXYUSERPWD, $this->proxy['user'] . ':' . $this->proxy['pass']);
|
|
|
667 |
}
|
|
|
668 |
}
|
|
|
669 |
|
|
|
670 |
// Set credentials for HTTP Basic/Digest Authentication.
|
|
|
671 |
if ($this->username && $this->password)
|
|
|
672 |
{
|
|
|
673 |
curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
|
|
|
674 |
curl_setopt($curl_handle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
|
|
|
675 |
}
|
|
|
676 |
|
|
|
677 |
// Handle the encoding if we can.
|
|
|
678 |
if (extension_loaded('zlib'))
|
|
|
679 |
{
|
|
|
680 |
curl_setopt($curl_handle, CURLOPT_ENCODING, 'gzip, deflate');
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
// Process custom headers
|
|
|
684 |
if (isset($this->request_headers) && count($this->request_headers))
|
|
|
685 |
{
|
|
|
686 |
$temp_headers = array();
|
|
|
687 |
|
|
|
688 |
foreach ($this->request_headers as $k => $v)
|
|
|
689 |
{
|
|
|
690 |
$temp_headers[] = $k . ': ' . $v;
|
|
|
691 |
}
|
|
|
692 |
|
|
|
693 |
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $temp_headers);
|
|
|
694 |
}
|
|
|
695 |
|
|
|
696 |
switch ($this->method)
|
|
|
697 |
{
|
|
|
698 |
case self::HTTP_PUT:
|
|
|
699 |
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT');
|
|
|
700 |
if (isset($this->read_stream))
|
|
|
701 |
{
|
|
|
702 |
if (!isset($this->read_stream_size) || $this->read_stream_size < 0)
|
|
|
703 |
{
|
|
|
704 |
throw new RequestCore_Exception('The stream size for the streaming upload cannot be determined.');
|
|
|
705 |
}
|
|
|
706 |
|
|
|
707 |
curl_setopt($curl_handle, CURLOPT_INFILESIZE, $this->read_stream_size);
|
|
|
708 |
curl_setopt($curl_handle, CURLOPT_UPLOAD, true);
|
|
|
709 |
}
|
|
|
710 |
else
|
|
|
711 |
{
|
|
|
712 |
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body);
|
|
|
713 |
}
|
|
|
714 |
break;
|
|
|
715 |
|
|
|
716 |
case self::HTTP_POST:
|
|
|
717 |
curl_setopt($curl_handle, CURLOPT_POST, true);
|
|
|
718 |
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body);
|
|
|
719 |
break;
|
|
|
720 |
|
|
|
721 |
case self::HTTP_HEAD:
|
|
|
722 |
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, self::HTTP_HEAD);
|
|
|
723 |
curl_setopt($curl_handle, CURLOPT_NOBODY, 1);
|
|
|
724 |
break;
|
|
|
725 |
|
|
|
726 |
default: // Assumed GET
|
|
|
727 |
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, $this->method);
|
|
|
728 |
if (isset($this->write_stream))
|
|
|
729 |
{
|
|
|
730 |
curl_setopt($curl_handle, CURLOPT_WRITEFUNCTION, array($this, 'streaming_write_callback'));
|
|
|
731 |
curl_setopt($curl_handle, CURLOPT_HEADER, false);
|
|
|
732 |
}
|
|
|
733 |
else
|
|
|
734 |
{
|
|
|
735 |
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body);
|
|
|
736 |
}
|
|
|
737 |
break;
|
|
|
738 |
}
|
|
|
739 |
|
|
|
740 |
// Merge in the CURLOPTs
|
|
|
741 |
if (isset($this->curlopts) && sizeof($this->curlopts) > 0)
|
|
|
742 |
{
|
|
|
743 |
foreach ($this->curlopts as $k => $v)
|
|
|
744 |
{
|
|
|
745 |
curl_setopt($curl_handle, $k, $v);
|
|
|
746 |
}
|
|
|
747 |
}
|
|
|
748 |
|
|
|
749 |
return $curl_handle;
|
|
|
750 |
}
|
|
|
751 |
|
|
|
752 |
/**
|
|
|
753 |
* Take the post-processed cURL data and break it down into useful header/body/info chunks. Uses the
|
|
|
754 |
* data stored in the `curl_handle` and `response` properties unless replacement data is passed in via
|
|
|
755 |
* parameters.
|
|
|
756 |
*
|
|
|
757 |
* @param resource $curl_handle (Optional) The reference to the already executed cURL request.
|
|
|
758 |
* @param string $response (Optional) The actual response content itself that needs to be parsed.
|
|
|
759 |
* @return ResponseCore A <ResponseCore> object containing a parsed HTTP response.
|
|
|
760 |
*/
|
|
|
761 |
public function process_response($curl_handle = null, $response = null)
|
|
|
762 |
{
|
|
|
763 |
// Accept a custom one if it's passed.
|
|
|
764 |
if ($curl_handle && $response)
|
|
|
765 |
{
|
|
|
766 |
$this->curl_handle = $curl_handle;
|
|
|
767 |
$this->response = $response;
|
|
|
768 |
}
|
|
|
769 |
|
|
|
770 |
// As long as this came back as a valid resource...
|
|
|
771 |
if (is_resource($this->curl_handle))
|
|
|
772 |
{
|
|
|
773 |
// Determine what's what.
|
|
|
774 |
$header_size = curl_getinfo($this->curl_handle, CURLINFO_HEADER_SIZE);
|
|
|
775 |
$this->response_headers = substr($this->response, 0, $header_size);
|
|
|
776 |
$this->response_body = substr($this->response, $header_size);
|
|
|
777 |
$this->response_code = curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE);
|
|
|
778 |
$this->response_info = curl_getinfo($this->curl_handle);
|
|
|
779 |
|
|
|
780 |
// Parse out the headers
|
|
|
781 |
$this->response_headers = explode("\r\n\r\n", trim($this->response_headers));
|
|
|
782 |
$this->response_headers = array_pop($this->response_headers);
|
|
|
783 |
$this->response_headers = explode("\r\n", $this->response_headers);
|
|
|
784 |
array_shift($this->response_headers);
|
|
|
785 |
|
|
|
786 |
// Loop through and split up the headers.
|
|
|
787 |
$header_assoc = array();
|
|
|
788 |
foreach ($this->response_headers as $header)
|
|
|
789 |
{
|
|
|
790 |
$kv = explode(': ', $header);
|
|
|
791 |
$header_assoc[strtolower($kv[0])] = $kv[1];
|
|
|
792 |
}
|
|
|
793 |
|
|
|
794 |
// Reset the headers to the appropriate property.
|
|
|
795 |
$this->response_headers = $header_assoc;
|
|
|
796 |
$this->response_headers['_info'] = $this->response_info;
|
|
|
797 |
$this->response_headers['_info']['method'] = $this->method;
|
|
|
798 |
|
|
|
799 |
if ($curl_handle && $response)
|
|
|
800 |
{
|
|
|
801 |
return new $this->response_class($this->response_headers, $this->response_body, $this->response_code, $this->curl_handle);
|
|
|
802 |
}
|
|
|
803 |
}
|
|
|
804 |
|
|
|
805 |
// Return false
|
|
|
806 |
return false;
|
|
|
807 |
}
|
|
|
808 |
|
|
|
809 |
/**
|
|
|
810 |
* Sends the request, calling necessary utility functions to update built-in properties.
|
|
|
811 |
*
|
|
|
812 |
* @param boolean $parse (Optional) Whether to parse the response with ResponseCore or not.
|
|
|
813 |
* @return string The resulting unparsed data from the request.
|
|
|
814 |
*/
|
|
|
815 |
public function send_request($parse = false)
|
|
|
816 |
{
|
|
|
817 |
set_time_limit(0);
|
|
|
818 |
|
|
|
819 |
$curl_handle = $this->prep_request();
|
|
|
820 |
$this->response = curl_exec($curl_handle);
|
|
|
821 |
|
|
|
822 |
if ($this->response === false)
|
|
|
823 |
{
|
|
|
824 |
throw new cURL_Exception('cURL resource: ' . (string) $curl_handle . '; cURL error: ' . curl_error($curl_handle) . ' (cURL error code ' . curl_errno($curl_handle) . '). See http://curl.haxx.se/libcurl/c/libcurl-errors.html for an explanation of error codes.');
|
|
|
825 |
}
|
|
|
826 |
|
|
|
827 |
$parsed_response = $this->process_response($curl_handle, $this->response);
|
|
|
828 |
|
|
|
829 |
curl_close($curl_handle);
|
|
|
830 |
|
|
|
831 |
if ($parse)
|
|
|
832 |
{
|
|
|
833 |
return $parsed_response;
|
|
|
834 |
}
|
|
|
835 |
|
|
|
836 |
return $this->response;
|
|
|
837 |
}
|
|
|
838 |
|
|
|
839 |
/**
|
|
|
840 |
* Sends the request using <php:curl_multi_exec()>, enabling parallel requests. Uses the "rolling" method.
|
|
|
841 |
*
|
|
|
842 |
* @param array $handles (Required) An indexed array of cURL handles to process simultaneously.
|
|
|
843 |
* @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
|
|
|
844 |
* <li><code>callback</code> - <code>string|array</code> - Optional - The string name of a function to pass the response data to. If this is a method, pass an array where the <code>[0]</code> index is the class and the <code>[1]</code> index is the method name.</li>
|
|
|
845 |
* <li><code>limit</code> - <code>integer</code> - Optional - The number of simultaneous requests to make. This can be useful for scaling around slow server responses. Defaults to trusting cURLs judgement as to how many to use.</li></ul>
|
|
|
846 |
* @return array Post-processed cURL responses.
|
|
|
847 |
*/
|
|
|
848 |
public function send_multi_request($handles, $opt = null)
|
|
|
849 |
{
|
|
|
850 |
set_time_limit(0);
|
|
|
851 |
|
|
|
852 |
// Skip everything if there are no handles to process.
|
|
|
853 |
if (count($handles) === 0) return array();
|
|
|
854 |
|
|
|
855 |
if (!$opt) $opt = array();
|
|
|
856 |
|
|
|
857 |
// Initialize any missing options
|
|
|
858 |
$limit = isset($opt['limit']) ? $opt['limit'] : -1;
|
|
|
859 |
|
|
|
860 |
// Initialize
|
|
|
861 |
$handle_list = $handles;
|
|
|
862 |
$http = new $this->request_class();
|
|
|
863 |
$multi_handle = curl_multi_init();
|
|
|
864 |
$handles_post = array();
|
|
|
865 |
$added = count($handles);
|
|
|
866 |
$last_handle = null;
|
|
|
867 |
$count = 0;
|
|
|
868 |
$i = 0;
|
|
|
869 |
|
|
|
870 |
// Loop through the cURL handles and add as many as it set by the limit parameter.
|
|
|
871 |
while ($i < $added)
|
|
|
872 |
{
|
|
|
873 |
if ($limit > 0 && $i >= $limit) break;
|
|
|
874 |
curl_multi_add_handle($multi_handle, array_shift($handles));
|
|
|
875 |
$i++;
|
|
|
876 |
}
|
|
|
877 |
|
|
|
878 |
do
|
|
|
879 |
{
|
|
|
880 |
$active = false;
|
|
|
881 |
|
|
|
882 |
// Start executing and wait for a response.
|
|
|
883 |
while (($status = curl_multi_exec($multi_handle, $active)) === CURLM_CALL_MULTI_PERFORM)
|
|
|
884 |
{
|
|
|
885 |
// Start looking for possible responses immediately when we have to add more handles
|
|
|
886 |
if (count($handles) > 0) break;
|
|
|
887 |
}
|
|
|
888 |
|
|
|
889 |
// Figure out which requests finished.
|
|
|
890 |
$to_process = array();
|
|
|
891 |
|
|
|
892 |
while ($done = curl_multi_info_read($multi_handle))
|
|
|
893 |
{
|
|
|
894 |
// Since curl_errno() isn't reliable for handles that were in multirequests, we check the 'result' of the info read, which contains the curl error number, (listed here http://curl.haxx.se/libcurl/c/libcurl-errors.html )
|
|
|
895 |
if ($done['result'] > 0)
|
|
|
896 |
{
|
|
|
897 |
throw new cURL_Multi_Exception('cURL resource: ' . (string) $done['handle'] . '; cURL error: ' . curl_error($done['handle']) . ' (cURL error code ' . $done['result'] . '). See http://curl.haxx.se/libcurl/c/libcurl-errors.html for an explanation of error codes.');
|
|
|
898 |
}
|
|
|
899 |
|
|
|
900 |
// Because curl_multi_info_read() might return more than one message about a request, we check to see if this request is already in our array of completed requests
|
|
|
901 |
elseif (!isset($to_process[(int) $done['handle']]))
|
|
|
902 |
{
|
|
|
903 |
$to_process[(int) $done['handle']] = $done;
|
|
|
904 |
}
|
|
|
905 |
}
|
|
|
906 |
|
|
|
907 |
// Actually deal with the request
|
|
|
908 |
foreach ($to_process as $pkey => $done)
|
|
|
909 |
{
|
|
|
910 |
$response = $http->process_response($done['handle'], curl_multi_getcontent($done['handle']));
|
|
|
911 |
$key = array_search($done['handle'], $handle_list, true);
|
|
|
912 |
$handles_post[$key] = $response;
|
|
|
913 |
|
|
|
914 |
if (count($handles) > 0)
|
|
|
915 |
{
|
|
|
916 |
curl_multi_add_handle($multi_handle, array_shift($handles));
|
|
|
917 |
}
|
|
|
918 |
|
|
|
919 |
curl_multi_remove_handle($multi_handle, $done['handle']);
|
|
|
920 |
curl_close($done['handle']);
|
|
|
921 |
}
|
|
|
922 |
}
|
|
|
923 |
while ($active || count($handles_post) < $added);
|
|
|
924 |
|
|
|
925 |
curl_multi_close($multi_handle);
|
|
|
926 |
|
|
|
927 |
ksort($handles_post, SORT_NUMERIC);
|
|
|
928 |
return $handles_post;
|
|
|
929 |
}
|
|
|
930 |
|
|
|
931 |
|
|
|
932 |
/*%******************************************************************************************%*/
|
|
|
933 |
// RESPONSE METHODS
|
|
|
934 |
|
|
|
935 |
/**
|
|
|
936 |
* Get the HTTP response headers from the request.
|
|
|
937 |
*
|
|
|
938 |
* @param string $header (Optional) A specific header value to return. Defaults to all headers.
|
|
|
939 |
* @return string|array All or selected header values.
|
|
|
940 |
*/
|
|
|
941 |
public function get_response_header($header = null)
|
|
|
942 |
{
|
|
|
943 |
if ($header)
|
|
|
944 |
{
|
|
|
945 |
return $this->response_headers[strtolower($header)];
|
|
|
946 |
}
|
|
|
947 |
return $this->response_headers;
|
|
|
948 |
}
|
|
|
949 |
|
|
|
950 |
/**
|
|
|
951 |
* Get the HTTP response body from the request.
|
|
|
952 |
*
|
|
|
953 |
* @return string The response body.
|
|
|
954 |
*/
|
|
|
955 |
public function get_response_body()
|
|
|
956 |
{
|
|
|
957 |
return $this->response_body;
|
|
|
958 |
}
|
|
|
959 |
|
|
|
960 |
/**
|
|
|
961 |
* Get the HTTP response code from the request.
|
|
|
962 |
*
|
|
|
963 |
* @return string The HTTP response code.
|
|
|
964 |
*/
|
|
|
965 |
public function get_response_code()
|
|
|
966 |
{
|
|
|
967 |
return $this->response_code;
|
|
|
968 |
}
|
|
|
969 |
}
|
|
|
970 |
|
|
|
971 |
|
|
|
972 |
/**
|
|
|
973 |
* Container for all response-related methods.
|
|
|
974 |
*/
|
|
|
975 |
class ResponseCore
|
|
|
976 |
{
|
|
|
977 |
/**
|
|
|
978 |
* Stores the HTTP header information.
|
|
|
979 |
*/
|
|
|
980 |
public $header;
|
|
|
981 |
|
|
|
982 |
/**
|
|
|
983 |
* Stores the SimpleXML response.
|
|
|
984 |
*/
|
|
|
985 |
public $body;
|
|
|
986 |
|
|
|
987 |
/**
|
|
|
988 |
* Stores the HTTP response code.
|
|
|
989 |
*/
|
|
|
990 |
public $status;
|
|
|
991 |
|
|
|
992 |
/**
|
|
|
993 |
* Constructs a new instance of this class.
|
|
|
994 |
*
|
|
|
995 |
* @param array $header (Required) Associative array of HTTP headers (typically returned by <RequestCore::get_response_header()>).
|
|
|
996 |
* @param string $body (Required) XML-formatted response from AWS.
|
|
|
997 |
* @param integer $status (Optional) HTTP response status code from the request.
|
|
|
998 |
* @return object Contains an <php:array> `header` property (HTTP headers as an associative array), a <php:SimpleXMLElement> or <php:string> `body` property, and an <php:integer> `status` code.
|
|
|
999 |
*/
|
|
|
1000 |
public function __construct($header, $body, $status = null)
|
|
|
1001 |
{
|
|
|
1002 |
$this->header = $header;
|
|
|
1003 |
$this->body = $body;
|
|
|
1004 |
$this->status = $status;
|
|
|
1005 |
|
|
|
1006 |
return $this;
|
|
|
1007 |
}
|
|
|
1008 |
|
|
|
1009 |
/**
|
|
|
1010 |
* Did we receive the status code we expected?
|
|
|
1011 |
*
|
|
|
1012 |
* @param integer|array $codes (Optional) The status code(s) to expect. Pass an <php:integer> for a single acceptable value, or an <php:array> of integers for multiple acceptable values.
|
|
|
1013 |
* @return boolean Whether we received the expected status code or not.
|
|
|
1014 |
*/
|
|
|
1015 |
public function isOK($codes = array(200, 201, 204, 206))
|
|
|
1016 |
{
|
|
|
1017 |
if (is_array($codes))
|
|
|
1018 |
{
|
|
|
1019 |
return in_array($this->status, $codes);
|
|
|
1020 |
}
|
|
|
1021 |
|
|
|
1022 |
return $this->status === $codes;
|
|
|
1023 |
}
|
|
|
1024 |
}
|
|
|
1025 |
|
|
|
1026 |
class cURL_Exception extends Exception {}
|
|
|
1027 |
class cURL_Multi_Exception extends cURL_Exception {}
|
|
|
1028 |
class RequestCore_Exception extends Exception {}
|