]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/Requests/Requests/Response.php
Add Requests 1.6
[Github/YOURLS.git] / includes / Requests / Requests / Response.php
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         }
22
23         /**
24          * Response body
25          * @var string
26          */
27         public $body = '';
28
29         /**
30          * Raw HTTP data from the transport
31          * @var string
32          */
33         public $raw = '';
34
35         /**
36          * Headers, as an associative array
37          * @var array
38          */
39         public $headers = array();
40
41         /**
42          * Status code, false if non-blocking
43          * @var integer|boolean
44          */
45         public $status_code = false;
46
47         /**
48          * Whether the request succeeded or not
49          * @var boolean
50          */
51         public $success = false;
52
53         /**
54          * Number of redirects the request used
55          * @var integer
56          */
57         public $redirects = 0;
58
59         /**
60          * URL requested
61          * @var string
62          */
63         public $url = '';
64
65         /**
66          * Previous requests (from redirects)
67          * @var array Array of Requests_Response objects
68          */
69         public $history = array();
70
71         /**
72          * Cookies from the request
73          */
74         public $cookies = array();
75
76         /**
77          * Throws an exception if the request was not successful
78          *
79          * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
80          * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})
81          * @param boolean $allow_redirects Set to false to throw on a 3xx as well
82          */
83         public function throw_for_status($allow_redirects = true) {
84                 if ($this->status_code >= 300 && $this->status_code < 400) {
85                         if (!$allow_redirects) {
86                                 throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);
87                         }
88                 }
89
90                 elseif (!$this->success) {
91                         $exception = Requests_Exception_HTTP::get_class($this->status_code);
92                         throw new $exception(null, $this);
93                 }
94         }
95 }