|
1 <?php |
|
2 /** |
|
3 * HTTP response class |
|
4 * |
|
5 * Contains a response from Requests::request() |
|
6 * @package Requests |
|
7 */ |
|
8 |
|
9 /** |
|
10 * HTTP response class |
|
11 * |
|
12 * Contains a response from Requests::request() |
|
13 * @package Requests |
|
14 */ |
|
15 class Requests_Response { |
|
16 /** |
|
17 * Constructor |
|
18 */ |
|
19 public function __construct() { |
|
20 $this->headers = new Requests_Response_Headers(); |
|
21 $this->cookies = new Requests_Cookie_Jar(); |
|
22 } |
|
23 |
|
24 /** |
|
25 * Response body |
|
26 * |
|
27 * @var string |
|
28 */ |
|
29 public $body = ''; |
|
30 |
|
31 /** |
|
32 * Raw HTTP data from the transport |
|
33 * |
|
34 * @var string |
|
35 */ |
|
36 public $raw = ''; |
|
37 |
|
38 /** |
|
39 * Headers, as an associative array |
|
40 * |
|
41 * @var Requests_Response_Headers Array-like object representing headers |
|
42 */ |
|
43 public $headers = array(); |
|
44 |
|
45 /** |
|
46 * Status code, false if non-blocking |
|
47 * |
|
48 * @var integer|boolean |
|
49 */ |
|
50 public $status_code = false; |
|
51 |
|
52 /** |
|
53 * Protocol version, false if non-blocking |
|
54 * @var float|boolean |
|
55 */ |
|
56 public $protocol_version = false; |
|
57 |
|
58 /** |
|
59 * Whether the request succeeded or not |
|
60 * |
|
61 * @var boolean |
|
62 */ |
|
63 public $success = false; |
|
64 |
|
65 /** |
|
66 * Number of redirects the request used |
|
67 * |
|
68 * @var integer |
|
69 */ |
|
70 public $redirects = 0; |
|
71 |
|
72 /** |
|
73 * URL requested |
|
74 * |
|
75 * @var string |
|
76 */ |
|
77 public $url = ''; |
|
78 |
|
79 /** |
|
80 * Previous requests (from redirects) |
|
81 * |
|
82 * @var array Array of Requests_Response objects |
|
83 */ |
|
84 public $history = array(); |
|
85 |
|
86 /** |
|
87 * Cookies from the request |
|
88 * |
|
89 * @var Requests_Cookie_Jar Array-like object representing a cookie jar |
|
90 */ |
|
91 public $cookies = array(); |
|
92 |
|
93 /** |
|
94 * Is the response a redirect? |
|
95 * |
|
96 * @return boolean True if redirect (3xx status), false if not. |
|
97 */ |
|
98 public function is_redirect() { |
|
99 $code = $this->status_code; |
|
100 return in_array($code, array(300, 301, 302, 303, 307)) || $code > 307 && $code < 400; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Throws an exception if the request was not successful |
|
105 * |
|
106 * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`) |
|
107 * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404}) |
|
108 * @param boolean $allow_redirects Set to false to throw on a 3xx as well |
|
109 */ |
|
110 public function throw_for_status($allow_redirects = true) { |
|
111 if ($this->is_redirect()) { |
|
112 if (!$allow_redirects) { |
|
113 throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this); |
|
114 } |
|
115 } |
|
116 elseif (!$this->success) { |
|
117 $exception = Requests_Exception_HTTP::get_class($this->status_code); |
|
118 throw new $exception(null, $this); |
|
119 } |
|
120 } |
|
121 } |