5 * Contains a response from Requests::request()
12 * Contains a response from Requests::request()
15 class Requests_Response {
19 public function __construct() {
20 $this->headers = new Requests_Response_Headers();
21 $this->cookies = new Requests_Cookie_Jar();
32 * Raw HTTP data from the transport
39 * Headers, as an associative array
41 * @var Requests_Response_Headers Array-like object representing headers
43 public $headers = array();
46 * Status code, false if non-blocking
48 * @var integer|boolean
50 public $status_code = false;
53 * Protocol version, false if non-blocking
56 public $protocol_version = false;
59 * Whether the request succeeded or not
63 public $success = false;
66 * Number of redirects the request used
70 public $redirects = 0;
80 * Previous requests (from redirects)
82 * @var array Array of Requests_Response objects
84 public $history = array();
87 * Cookies from the request
89 * @var Requests_Cookie_Jar Array-like object representing a cookie jar
91 public $cookies = array();
94 * Is the response a redirect?
96 * @return boolean True if redirect (3xx status), false if not.
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;
104 * Throws an exception if the request was not successful
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
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);
116 elseif (!$this->success) {
117 $exception = Requests_Exception_HTTP::get_class($this->status_code);
118 throw new $exception(null, $this);