-
Notifications
You must be signed in to change notification settings - Fork 5
/
requestcore.class.php
executable file
·907 lines (796 loc) · 21.8 KB
/
requestcore.class.php
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
<?php
/**
* File: RequestCore
* Handles all HTTP requests using cURL and manages the responses.
*
* Version:
* 2010.10.11
*
* Copyright:
* 2006-2010 Ryan Parman, Foleeo Inc., and contributors.
*
* License:
* Simplified BSD License - http://opensource.org/licenses/bsd-license.php
*/
/*%******************************************************************************************%*/
// EXCEPTIONS
/**
* Exception: RequestCore_Exception
* Default RequestCore Exception.
*/
class RequestCore_Exception extends Exception {}
/*%******************************************************************************************%*/
// CLASS
/**
* Class: RequestCore
* Container for all request-related methods.
*/
class RequestCore
{
/**
* Property: request_url
* The URL being requested.
*/
var $request_url;
/**
* Property: request_headers
* The headers being sent in the request.
*/
var $request_headers;
/**
* Property: request_body
* The body being sent in the request.
*/
var $request_body;
/**
* Property: response
* The response returned by the request.
*/
var $response;
/**
* Property: response_headers
* The headers returned by the request.
*/
var $response_headers;
/**
* Property: response_body
* The body returned by the request.
*/
var $response_body;
/**
* Property: response_code
* The HTTP status code returned by the request.
*/
var $response_code;
/**
* Property: response_info
* Additional response data.
*/
var $response_info;
/**
* Property: curl_handle
* The handle for the cURL object.
*/
var $curl_handle;
/**
* Property: method
* The method by which the request is being made.
*/
var $method;
/**
* Property: proxy
* Stores the proxy settings to use for the request.
*/
var $proxy = null;
/**
* Property: username
* The username to use for the request.
*/
var $username = null;
/**
* Property: password
* The password to use for the request.
*/
var $password = null;
/**
* Property: curlopts
* Custom CURLOPT settings.
*/
var $curlopts = null;
/**
* Property: request_class
* The default class to use for HTTP Requests (defaults to <RequestCore>).
*/
var $request_class = 'RequestCore';
/**
* Property: response_class
* The default class to use for HTTP Responses (defaults to <ResponseCore>).
*/
var $response_class = 'ResponseCore';
/**
* Property: useragent
* Default useragent string to use.
*/
var $useragent = 'RequestCore/1.2';
/**
* Property: read_file
* File to read from while streaming up.
*/
var $read_file = null;
/**
* Property: write_file
* File to write to while streaming down.
*/
var $write_file = null;
/*%******************************************************************************************%*/
// CONSTANTS
/**
* Constant: HTTP_GET
* GET HTTP Method
*/
const HTTP_GET = 'GET';
/**
* Constant: HTTP_POST
* POST HTTP Method
*/
const HTTP_POST = 'POST';
/**
* Constant: HTTP_PUT
* PUT HTTP Method
*/
const HTTP_PUT = 'PUT';
/**
* Constant: HTTP_DELETE
* DELETE HTTP Method
*/
const HTTP_DELETE = 'DELETE';
/**
* Constant: HTTP_HEAD
* HEAD HTTP Method
*/
const HTTP_HEAD = 'HEAD';
/*%******************************************************************************************%*/
// CONSTRUCTOR
/**
* Method: __construct()
* The constructor
*
* Access:
* public
*
* Parameters:
* $url - _string_ (Optional) The URL to request or service endpoint to query.
* $proxy - _string_ (Optional) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port`
* $helpers - _array_ (Optional) An associative array of classnames to use for request, and response functionality. Gets passed in automatically by the calling class.
*
* Returns:
* `$this`
*/
public function __construct($url = null, $proxy = null, $helpers = null)
{
// Set some default values.
$this->request_url = $url;
$this->method = self::HTTP_GET;
$this->request_headers = array();
$this->request_body = '';
// Set a new Request class if one was set.
if (isset($helpers['request']) && !empty($helpers['request']))
{
$this->request_class = $helpers['request'];
}
// Set a new Request class if one was set.
if (isset($helpers['response']) && !empty($helpers['response']))
{
$this->response_class = $helpers['response'];
}
if ($proxy)
{
$this->set_proxy($proxy);
}
return $this;
}
/*%******************************************************************************************%*/
// REQUEST METHODS
/**
* Method: set_credentials()
* Sets the credentials to use for authentication.
*
* Access:
* public
*
* Parameters:
* $user - _string_ (Required) The username to authenticate with.
* $pass - _string_ (Required) The password to authenticate with.
*
* Returns:
* `$this`
*/
public function set_credentials($user, $pass)
{
$this->username = $user;
$this->password = $pass;
return $this;
}
/**
* Method: add_header()
* Adds a custom HTTP header to the cURL request.
*
* Access:
* public
*
* Parameters:
* $key - _string_ (Required) The custom HTTP header to set.
* $value - _mixed_ (Required) The value to assign to the custom HTTP header.
*
* Returns:
* `$this`
*/
public function add_header($key, $value)
{
$this->request_headers[$key] = $value;
return $this;
}
/**
* Method: remove_header()
* Removes an HTTP header from the cURL request.
*
* Access:
* public
*
* Parameters:
* $key - _string_ (Required) The custom HTTP header to set.
*
* Returns:
* `$this`
*/
public function remove_header($key)
{
if (isset($this->request_headers[$key]))
{
unset($this->request_headers[$key]);
}
return $this;
}
/**
* Method: set_method()
* Set the method type for the request.
*
* Access:
* public
*
* Parameters:
* $method - _string_ (Required) One of the following constants: <HTTP_GET>, <HTTP_POST>, <HTTP_PUT>, <HTTP_HEAD>, <HTTP_DELETE>.
*
* Returns:
* `$this`
*/
public function set_method($method)
{
$this->method = strtoupper($method);
return $this;
}
/**
* Method: set_useragent()
* Sets a custom useragent string for the class.
*
* Access:
* public
*
* Parameters:
* $ua - _string_ (Required) The useragent string to use.
*
* Returns:
* `$this`
*/
public function set_useragent($ua)
{
$this->useragent = $ua;
return $this;
}
/**
* Method: set_body()
* Set the body to send in the request.
*
* Access:
* public
*
* Parameters:
* $body - _string_ (Required) The textual content to send along in the body of the request.
*
* Returns:
* `$this`
*/
public function set_body($body)
{
$this->request_body = $body;
return $this;
}
/**
* Method: set_request_url()
* Set the URL to make the request to.
*
* Access:
* public
*
* Parameters:
* $url - _string_ (Required) The URL to make the request to.
*
* Returns:
* `$this`
*/
public function set_request_url($url)
{
$this->request_url = $url;
return $this;
}
/**
* Method: set_curlopts()
* Set additional CURLOPT settings. These will merge with the default settings, and override if there is a duplicate.
*
* Access:
* public
*
* Parameters:
* $curlopts - _array_ (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.
*
* Returns:
* `$this`
*/
public function set_curlopts($curlopts)
{
$this->curlopts = $curlopts;
return $this;
}
/**
* Method: set_read_file()
* Sets the file to read from while streaming up.
*
* Access:
* public
*
* Parameters:
* $location - _string_ (Required) The file system location to read from.
*
* Returns:
* `$this`
*/
public function set_read_file($location)
{
$this->read_file = $location;
return $this;
}
/**
* Method: set_write_file()
* Sets the file to write to while streaming down.
*
* Access:
* public
*
* Parameters:
* $location - _string_ (Required) The file system location to read from.
*
* Returns:
* `$this`
*/
public function set_write_file($location)
{
$this->write_file = $location;
return $this;
}
/**
* Method: set_proxy()
* Set the proxy to use for making requests.
*
* Access:
* public
*
* Parameters:
* $proxy - _string_ (Optional) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port`
*
* Returns:
* `$this`
*/
public function set_proxy($proxy)
{
$proxy = parse_url($proxy);
$proxy['user'] = isset($proxy['user']) ? $proxy['user'] : null;
$proxy['pass'] = isset($proxy['pass']) ? $proxy['pass'] : null;
$proxy['port'] = isset($proxy['port']) ? $proxy['port'] : null;
$this->proxy = $proxy;
return $this;
}
/*%******************************************************************************************%*/
// PREPARE, SEND, AND PROCESS REQUEST
/**
* Method: prep_request()
* Prepares and adds the details of the cURL request. This can be passed along to a `curl_multi_exec()` function.
*
* Access:
* public
*
* Returns:
* The handle for the cURL object.
*/
public function prep_request()
{
$curl_handle = curl_init();
// Set default options.
curl_setopt($curl_handle, CURLOPT_URL, $this->request_url);
curl_setopt($curl_handle, CURLOPT_FILETIME, true);
curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, false);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl_handle, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5);
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5184000);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($curl_handle, CURLOPT_NOSIGNAL, true);
curl_setopt($curl_handle, CURLOPT_REFERER, $this->request_url);
curl_setopt($curl_handle, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($curl_handle, CURLOPT_LOW_SPEED_LIMIT, 1);
// Enable a proxy connection if requested.
if ($this->proxy)
{
curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, true);
$host = $this->proxy['host'];
$host .= ($this->proxy['port']) ? ':' . $this->proxy['port'] : '';
curl_setopt($curl_handle, CURLOPT_PROXY, $host);
if (isset($this->proxy['user']) && isset($this->proxy['pass']))
{
curl_setopt($curl_handle, CURLOPT_PROXYUSERPWD, $this->proxy['user'] . ':' . $this->proxy['pass']);
}
}
// Set credentials for HTTP Basic/Digest Authentication.
if ($this->username && $this->password)
{
curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl_handle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
}
// Handle the encoding if we can.
if (extension_loaded('zlib'))
{
curl_setopt($curl_handle, CURLOPT_ENCODING, '');
}
// Process custom headers
if (isset($this->request_headers) && count($this->request_headers))
{
$temp_headers = array();
foreach ($this->request_headers as $k => $v)
{
$temp_headers[] = $k . ': ' . $v;
}
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $temp_headers);
}
switch ($this->method)
{
case self::HTTP_PUT:
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body);
if ($this->read_file)
{
// @todo: Close this connection!
$read_file_handle = fopen($this->read_file, 'r');
curl_setopt($curl_handle, CURLOPT_INFILE, $read_file_handle);
curl_setopt($curl_handle, CURLOPT_INFILESIZE, filesize($this->read_file));
curl_setopt($curl_handle, CURLOPT_UPLOAD, true);
}
break;
case self::HTTP_POST:
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body);
break;
case self::HTTP_HEAD:
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, self::HTTP_HEAD);
curl_setopt($curl_handle, CURLOPT_NOBODY, 1);
break;
default: // Assumed GET
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, $this->method);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body);
if ($this->write_file)
{
// @todo: Close this connection!
$write_file_handle = fopen($this->write_file, 'w+');
curl_setopt($curl_handle, CURLOPT_FILE, $write_file_handle);
curl_setopt($curl_handle, CURLOPT_HEADER, false);
}
break;
}
// Merge in the CURLOPTs
if (isset($this->curlopts) && sizeof($this->curlopts) > 0)
{
foreach ($this->curlopts as $k => $v)
{
curl_setopt($curl_handle, $k, $v);
}
}
return $curl_handle;
}
/**
* Method: process_response()
* Take the post-processed cURL data and break it down into useful header/body/info chunks. Uses the data stored in the <curl_handle> and <response> properties unless replacement data is passed in via parameters.
*
* Access:
* public
*
* Parameters:
* $curl_handle - _string_ (Optional) The reference to the already executed cURL request.
* $response - _string_ (Optional) The actual response content itself that needs to be parsed.
*
* Returns:
* <ResponseCore> object
*/
public function process_response($curl_handle = null, $response = null)
{
// Accept a custom one if it's passed.
if ($curl_handle && $response)
{
$this->curl_handle = $curl_handle;
$this->response = $response;
}
// As long as this came back as a valid resource...
if (is_resource($this->curl_handle))
{
// Determine what's what.
$header_size = curl_getinfo($this->curl_handle, CURLINFO_HEADER_SIZE);
$this->response_headers = substr($this->response, 0, $header_size);
$this->response_body = substr($this->response, $header_size);
$this->response_code = curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE);
$this->response_info = curl_getinfo($this->curl_handle);
// Parse out the headers
$this->response_headers = explode("\r\n\r\n", trim($this->response_headers));
$this->response_headers = array_pop($this->response_headers);
$this->response_headers = explode("\r\n", $this->response_headers);
array_shift($this->response_headers);
// Loop through and split up the headers.
$header_assoc = array();
foreach ($this->response_headers as $header)
{
$kv = explode(': ', $header);
$header_assoc[strtolower($kv[0])] = $kv[1];
}
// Reset the headers to the appropriate property.
$this->response_headers = $header_assoc;
$this->response_headers['_info'] = $this->response_info;
$this->response_headers['_info']['method'] = $this->method;
if ($curl_handle && $response)
{
return new $this->response_class($this->response_headers, $this->response_body, $this->response_code, $this->curl_handle);
}
}
// Return false
return false;
}
/**
* Method: send_request()
* Sends the request, calling necessary utility functions to update built-in properties.
*
* Access:
* public
*
* Parameters:
* $parse - _boolean_ (Optional) Whether to parse the response with ResponseCore or not.
*
* Returns:
* _string_ The resulting unparsed data from the request.
*/
public function send_request($parse = false)
{
set_time_limit(0);
$curl_handle = $this->prep_request();
$this->response = curl_exec($curl_handle);
$parsed_response = $this->process_response($curl_handle, $this->response);
curl_close($curl_handle);
if ($parse)
{
return $parsed_response;
}
return $this->response;
}
/**
* Method: send_multi_request()
* Sends the request using curl_multi_exec(), enabling parallel requests. Uses the "rolling" method.
*
* Access:
* public
*
* Parameters:
* $handles - _array_ (Required) An indexed array of cURL handles to process simultaneously.
* $opt - _array_ (Optional) Associative array of parameters which can have the following keys:
*
* Keys for the $opt parameter:
* callback - _string_|_array_ (Optional) The string name of a function to pass the response data to. If this is a method, pass an array where the [0] index is the class and the [1] index is the method name.
* limit - _integer_ (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.
*
* Returns:
* _array_ Post-processed cURL responses.
*/
public function send_multi_request($handles, $opt = null)
{
// Skip everything if there are no handles to process.
if (count($handles) === 0) return array();
if (!$opt) $opt = array();
// Initialize any missing options
$limit = isset($opt['limit']) ? $opt['limit'] : -1;
// Initialize
$handle_list = $handles;
$http = new $this->request_class();
$multi_handle = curl_multi_init();
$handles_post = array();
$added = count($handles);
$last_handle = null;
$count = 0;
$i = 0;
// Loop through the cURL handles and add as many as it set by the limit parameter.
while ($i < $added)
{
if ($limit > 0 && $i >= $limit) break;
curl_multi_add_handle($multi_handle, array_shift($handles));
$i++;
}
do
{
$active = false;
// Start executing and wait for a response.
while (($status = curl_multi_exec($multi_handle, $active)) === CURLM_CALL_MULTI_PERFORM)
{
if ($status !== CURLM_OK && $limit > 0) break;
}
// Figure out which requests finished.
$qlength = 0;
$to_process = array();
while ($done = curl_multi_info_read($multi_handle, $qlength))
{
// 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 )
if ($done['result'] > 0)
{
throw new RequestCore_Exception('cURL resource: ' . (string) $done['handle'] . '; cURL error: ' . curl_error($done['handle']) . ' (' . $done['result'] . ')');
}
// 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
elseif (!isset($to_process[(int) $done['handle']]))
{
$to_process[(int) $done['handle']] = $done;
}
}
// Actually deal with the request
foreach ($to_process as $pkey => $done)
{
if ((int) $done['handle'] != $last_handle)
{
$last_handle = (int) $done['handle'];
$response = $http->process_response($done['handle'], curl_multi_getcontent($done['handle']));
$key = array_search($done['handle'], $handle_list, true);
$handles_post[$key] = $response;
if (count($handles) > 0)
{
curl_multi_add_handle($multi_handle, array_shift($handles));
}
curl_multi_remove_handle($multi_handle, $done['handle']);
curl_close($done['handle']);
}
}
}
while ($active);
curl_multi_close($multi_handle);
ksort($handles_post, SORT_NUMERIC);
return $handles_post;
}
/*%******************************************************************************************%*/
// RESPONSE METHODS
/**
* Method: get_response_header()
* Get the HTTP response headers from the request.
*
* Access:
* public
*
* Parameters:
* $header - _string_ (Optional) A specific header value to return. Defaults to all headers.
*
* Returns:
* _string_|_array_ All or selected header values.
*/
public function get_response_header($header = null)
{
if ($header)
{
return $this->response_headers[strtolower($header)];
}
return $this->response_headers;
}
/**
* Method: get_response_body()
* Get the HTTP response body from the request.
*
* Access:
* public
*
* Returns:
* _string_ The response body.
*/
public function get_response_body()
{
return $this->response_body;
}
/**
* Method: get_response_code()
* Get the HTTP response code from the request.
*
* Access:
* public
*
* Returns:
* _string_ The HTTP response code.
*/
public function get_response_code()
{
return $this->response_code;
}
}
/**
* Class: ResponseCore
* Container for all response-related methods.
*/
class ResponseCore
{
/**
* Property: header
* Stores the HTTP header information.
*/
var $header;
/**
* Property: body
* Stores the SimpleXML response.
*/
var $body;
/**
* Property: status
* Stores the HTTP response code.
*/
var $status;
/**
* Method: __construct()
* The constructor
*
* Access:
* public
*
* Parameters:
* $header - _array_ (Required) Associative array of HTTP headers (typically returned by <RequestCore::get_response_header()>).
* $body - _string_ (Required) XML-formatted response from AWS.
* $status - _integer_ (Optional) HTTP response status code from the request.
*
* Returns:
* _object_ Contains an _array_ `header` property (HTTP headers as an associative array), a _SimpleXMLElement_ or _string_ `body` property, and an _integer_ `status` code.
*/
public function __construct($header, $body, $status = null)
{
$this->header = $header;
$this->body = $body;
$this->status = $status;
return $this;
}
/**
* Method: isOK()
* Did we receive the status code we expected?
*
* Access:
* public
*
* Parameters:
* $codes - _integer_|_array_ (Optional) The status code(s) to expect. Pass an _integer_ for a single acceptable value, or an _array_ of integers for multiple acceptable values. Defaults to _array_.
*
* Returns:
* _boolean_ Whether we received the expected status code or not.
*/
public function isOK($codes = array(200, 201, 204, 206))
{
if (is_array($codes))
{
return in_array($this->status, $codes);
}
return $this->status === $codes;
}
}