Changeset 52244 for trunk/src/wp-includes/Requests/Response.php
- Timestamp:
- 11/25/2021 01:10:30 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Response.php
r50842 r52244 3 3 * HTTP response class 4 4 * 5 * Contains a response from Requests::request() 5 * Contains a response from \WpOrg\Requests\Requests::request() 6 * 6 7 * @package Requests 7 8 */ 9 10 namespace WpOrg\Requests; 11 12 use WpOrg\Requests\Cookie\Jar; 13 use WpOrg\Requests\Exception; 14 use WpOrg\Requests\Exception\Http; 15 use WpOrg\Requests\Response\Headers; 8 16 9 17 /** 10 18 * HTTP response class 11 19 * 12 * Contains a response from Requests::request() 20 * Contains a response from \WpOrg\Requests\Requests::request() 21 * 13 22 * @package Requests 14 23 */ 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 } 24 class Response { 23 25 24 26 /** … … 39 41 * Headers, as an associative array 40 42 * 41 * @var Requests_Response_Headers Array-like object representing headers43 * @var \WpOrg\Requests\Response\Headers Array-like object representing headers 42 44 */ 43 public $headers = array();45 public $headers = []; 44 46 45 47 /** … … 81 83 * Previous requests (from redirects) 82 84 * 83 * @var array Array of Requests_Response objects85 * @var array Array of \WpOrg\Requests\Response objects 84 86 */ 85 public $history = array();87 public $history = []; 86 88 87 89 /** 88 90 * Cookies from the request 89 91 * 90 * @var Requests_Cookie_Jar Array-like object representing a cookie jar92 * @var \WpOrg\Requests\Cookie\Jar Array-like object representing a cookie jar 91 93 */ 92 public $cookies = array(); 94 public $cookies = []; 95 96 /** 97 * Constructor 98 */ 99 public function __construct() { 100 $this->headers = new Headers(); 101 $this->cookies = new Jar(); 102 } 93 103 94 104 /** … … 99 109 public function is_redirect() { 100 110 $code = $this->status_code; 101 return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400;111 return in_array($code, [300, 301, 302, 303, 307], true) || $code > 307 && $code < 400; 102 112 } 103 113 … … 105 115 * Throws an exception if the request was not successful 106 116 * 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 117 * @param boolean $allow_redirects Set to false to throw on a 3xx as well 118 * 119 * @throws \WpOrg\Requests\Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`) 120 * @throws \WpOrg\Requests\Exception\Http On non-successful status code. Exception class corresponds to "Status" + code (e.g. {@see \WpOrg\Requests\Exception\Http\Status404}) 110 121 */ 111 122 public function throw_for_status($allow_redirects = true) { 112 123 if ($this->is_redirect()) { 113 if ( !$allow_redirects) {114 throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);124 if ($allow_redirects !== true) { 125 throw new Exception('Redirection not allowed', 'response.no_redirects', $this); 115 126 } 116 127 } 117 128 elseif (!$this->success) { 118 $exception = Requests_Exception_HTTP::get_class($this->status_code);129 $exception = Http::get_class($this->status_code); 119 130 throw new $exception(null, $this); 120 131 } 121 132 } 133 134 /** 135 * JSON decode the response body. 136 * 137 * The method parameters are the same as those for the PHP native `json_decode()` function. 138 * 139 * @link https://php.net/json-decode 140 * 141 * @param ?bool $associative Optional. When `true`, JSON objects will be returned as associative arrays; 142 * When `false`, JSON objects will be returned as objects. 143 * When `null`, JSON objects will be returned as associative arrays 144 * or objects depending on whether `JSON_OBJECT_AS_ARRAY` is set in the flags. 145 * Defaults to `true` (in contrast to the PHP native default of `null`). 146 * @param int $depth Optional. Maximum nesting depth of the structure being decoded. 147 * Defaults to `512`. 148 * @param int $options Optional. Bitmask of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, 149 * JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR. 150 * Defaults to `0` (no options set). 151 * 152 * @return array 153 * 154 * @throws \WpOrg\Requests\Exception If `$this->body` is not valid json. 155 */ 156 public function decode_body($associative = true, $depth = 512, $options = 0) { 157 $data = json_decode($this->body, $associative, $depth, $options); 158 159 if (json_last_error() !== JSON_ERROR_NONE) { 160 $last_error = json_last_error_msg(); 161 throw new Exception('Unable to parse JSON data: ' . $last_error, 'response.invalid', $this); 162 } 163 164 return $data; 165 } 122 166 }
Note: See TracChangeset
for help on using the changeset viewer.