Auth
5 months ago
Cookie
5 months ago
Exception
5 months ago
Proxy
5 months ago
Response
5 months ago
Transport
5 months ago
Utility
5 months ago
Auth.php
5 months ago
Autoload.php
5 months ago
Capability.php
5 months ago
Cookie.php
5 months ago
Exception.php
5 months ago
HookManager.php
5 months ago
Hooks.php
5 months ago
IdnaEncoder.php
5 months ago
Ipv6.php
5 months ago
Iri.php
5 months ago
Port.php
5 months ago
Proxy.php
5 months ago
Requests.php
5 months ago
Response.php
5 months ago
Session.php
5 months ago
Ssl.php
5 months ago
Transport.php
5 months ago
Response.php
166 lines
| 1 | <?php |
| 2 | /** |
| 3 | * HTTP response class |
| 4 | * |
| 5 | * Contains a response from \WpOrg\Requests\Requests::request() |
| 6 | * |
| 7 | * @package AmeliaVendor_Requests |
| 8 | */ |
| 9 | |
| 10 | namespace AmeliaVendor\WpOrg\Requests; |
| 11 | |
| 12 | use AmeliaVendor\WpOrg\Requests\Cookie\Jar; |
| 13 | use AmeliaVendor\WpOrg\Requests\Exception; |
| 14 | use AmeliaVendor\WpOrg\Requests\Exception\Http; |
| 15 | use AmeliaVendor\WpOrg\Requests\Response\Headers; |
| 16 | |
| 17 | /** |
| 18 | * HTTP response class |
| 19 | * |
| 20 | * Contains a response from \WpOrg\Requests\Requests::request() |
| 21 | * |
| 22 | * @package Requests |
| 23 | */ |
| 24 | class Response { |
| 25 | |
| 26 | /** |
| 27 | * Response body |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | public $body = ''; |
| 32 | |
| 33 | /** |
| 34 | * Raw HTTP data from the transport |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $raw = ''; |
| 39 | |
| 40 | /** |
| 41 | * Headers, as an associative array |
| 42 | * |
| 43 | * @var \AmeliaVendor\WpOrg\Requests\Response\Headers Array-like object representing headers |
| 44 | */ |
| 45 | public $headers = []; |
| 46 | |
| 47 | /** |
| 48 | * Status code, false if non-blocking |
| 49 | * |
| 50 | * @var integer|boolean |
| 51 | */ |
| 52 | public $status_code = false; |
| 53 | |
| 54 | /** |
| 55 | * Protocol version, false if non-blocking |
| 56 | * |
| 57 | * @var float|boolean |
| 58 | */ |
| 59 | public $protocol_version = false; |
| 60 | |
| 61 | /** |
| 62 | * Whether the request succeeded or not |
| 63 | * |
| 64 | * @var boolean |
| 65 | */ |
| 66 | public $success = false; |
| 67 | |
| 68 | /** |
| 69 | * Number of redirects the request used |
| 70 | * |
| 71 | * @var integer |
| 72 | */ |
| 73 | public $redirects = 0; |
| 74 | |
| 75 | /** |
| 76 | * URL requested |
| 77 | * |
| 78 | * @var string |
| 79 | */ |
| 80 | public $url = ''; |
| 81 | |
| 82 | /** |
| 83 | * Previous requests (from redirects) |
| 84 | * |
| 85 | * @var array Array of \WpOrg\Requests\Response objects |
| 86 | */ |
| 87 | public $history = []; |
| 88 | |
| 89 | /** |
| 90 | * Cookies from the request |
| 91 | * |
| 92 | * @var \AmeliaVendor\WpOrg\Requests\Cookie\Jar Array-like object representing a cookie jar |
| 93 | */ |
| 94 | public $cookies = []; |
| 95 | |
| 96 | /** |
| 97 | * Constructor |
| 98 | */ |
| 99 | public function __construct() { |
| 100 | $this->headers = new Headers(); |
| 101 | $this->cookies = new Jar(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Is the response a redirect? |
| 106 | * |
| 107 | * @return boolean True if redirect (3xx status), false if not. |
| 108 | */ |
| 109 | public function is_redirect() { |
| 110 | $code = $this->status_code; |
| 111 | return in_array($code, [300, 301, 302, 303, 307], true) || $code > 307 && $code < 400; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Throws an exception if the request was not successful |
| 116 | * |
| 117 | * @param boolean $allow_redirects Set to false to throw on a 3xx as well |
| 118 | * |
| 119 | * @throws \AmeliaVendor\WpOrg\Requests\Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`) |
| 120 | * @throws \AmeliaVendor\WpOrg\Requests\Exception\Http On non-successful status code. Exception class corresponds to "Status" + code (e.g. {@see \WpOrg\Requests\Exception\Http\Status404}) |
| 121 | */ |
| 122 | public function throw_for_status($allow_redirects = true) { |
| 123 | if ($this->is_redirect()) { |
| 124 | if ($allow_redirects !== true) { |
| 125 | throw new Exception('Redirection not allowed', 'response.no_redirects', $this); |
| 126 | } |
| 127 | } elseif (!$this->success) { |
| 128 | $exception = Http::get_class($this->status_code); |
| 129 | throw new $exception(null, $this); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * JSON decode the response body. |
| 135 | * |
| 136 | * The method parameters are the same as those for the PHP native `json_decode()` function. |
| 137 | * |
| 138 | * @link https://php.net/json-decode |
| 139 | * |
| 140 | * @param bool|null $associative Optional. When `true`, JSON objects will be returned as associative arrays; |
| 141 | * When `false`, JSON objects will be returned as objects. |
| 142 | * When `null`, JSON objects will be returned as associative arrays |
| 143 | * or objects depending on whether `JSON_OBJECT_AS_ARRAY` is set in the flags. |
| 144 | * Defaults to `true` (in contrast to the PHP native default of `null`). |
| 145 | * @param int $depth Optional. Maximum nesting depth of the structure being decoded. |
| 146 | * Defaults to `512`. |
| 147 | * @param int $options Optional. Bitmask of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, |
| 148 | * JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR. |
| 149 | * Defaults to `0` (no options set). |
| 150 | * |
| 151 | * @return array |
| 152 | * |
| 153 | * @throws \AmeliaVendor\WpOrg\Requests\Exception If `$this->body` is not valid json. |
| 154 | */ |
| 155 | public function decode_body($associative = true, $depth = 512, $options = 0) { |
| 156 | $data = json_decode($this->body, $associative, $depth, $options); |
| 157 | |
| 158 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 159 | $last_error = json_last_error_msg(); |
| 160 | throw new Exception('Unable to parse JSON data: ' . $last_error, 'response.invalid', $this); |
| 161 | } |
| 162 | |
| 163 | return $data; |
| 164 | } |
| 165 | } |
| 166 |