|
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
|
|
|
* |
|
55
|
|
|
* @var float|boolean |
|
56
|
|
|
*/ |
|
57
|
|
|
public $protocol_version = false; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Whether the request succeeded or not |
|
61
|
|
|
* |
|
62
|
|
|
* @var boolean |
|
63
|
|
|
*/ |
|
64
|
|
|
public $success = false; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Number of redirects the request used |
|
68
|
|
|
* |
|
69
|
|
|
* @var integer |
|
70
|
|
|
*/ |
|
71
|
|
|
public $redirects = 0; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* URL requested |
|
75
|
|
|
* |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
public $url = ''; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Previous requests (from redirects) |
|
82
|
|
|
* |
|
83
|
|
|
* @var array Array of Requests_Response objects |
|
84
|
|
|
*/ |
|
85
|
|
|
public $history = array(); |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Cookies from the request |
|
89
|
|
|
* |
|
90
|
|
|
* @var Requests_Cookie_Jar Array-like object representing a cookie jar |
|
91
|
|
|
*/ |
|
92
|
|
|
public $cookies = array(); |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Is the response a redirect? |
|
96
|
|
|
* |
|
97
|
|
|
* @return boolean True if redirect (3xx status), false if not. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function is_redirect() { |
|
100
|
|
|
$code = $this->status_code; |
|
101
|
|
|
return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Throws an exception if the request was not successful |
|
106
|
|
|
* |
|
107
|
|
|
* @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`) |
|
108
|
|
|
* @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404}) |
|
109
|
|
|
* @param boolean $allow_redirects Set to false to throw on a 3xx as well |
|
110
|
|
|
*/ |
|
111
|
|
|
public function throw_for_status($allow_redirects = true) { |
|
112
|
|
|
if ($this->is_redirect()) { |
|
113
|
|
|
if (!$allow_redirects) { |
|
114
|
|
|
throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
elseif (!$this->success) { |
|
118
|
|
|
$exception = Requests_Exception_HTTP::get_class($this->status_code); |
|
119
|
|
|
throw new $exception(null, $this); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|