Changeset 52328 for trunk/src/wp-includes/Requests/Response.php
- Timestamp:
- 12/06/2021 09:29:00 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Response.php
r52244 r52328 3 3 * HTTP response class 4 4 * 5 * Contains a response from \WpOrg\Requests\Requests::request() 6 * 5 * Contains a response from Requests::request() 7 6 * @package Requests 8 7 */ 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;16 8 17 9 /** 18 10 * HTTP response class 19 11 * 20 * Contains a response from \WpOrg\Requests\Requests::request() 21 * 12 * Contains a response from Requests::request() 22 13 * @package Requests 23 14 */ 24 class Response { 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 } 25 23 26 24 /** … … 41 39 * Headers, as an associative array 42 40 * 43 * @var \WpOrg\Requests\Response\Headers Array-like object representing headers41 * @var Requests_Response_Headers Array-like object representing headers 44 42 */ 45 public $headers = [];43 public $headers = array(); 46 44 47 45 /** … … 83 81 * Previous requests (from redirects) 84 82 * 85 * @var array Array of \WpOrg\Requests\Response objects83 * @var array Array of Requests_Response objects 86 84 */ 87 public $history = [];85 public $history = array(); 88 86 89 87 /** 90 88 * Cookies from the request 91 89 * 92 * @var \WpOrg\Requests\Cookie\Jar Array-like object representing a cookie jar90 * @var Requests_Cookie_Jar Array-like object representing a cookie jar 93 91 */ 94 public $cookies = []; 95 96 /** 97 * Constructor 98 */ 99 public function __construct() { 100 $this->headers = new Headers(); 101 $this->cookies = new Jar(); 102 } 92 public $cookies = array(); 103 93 104 94 /** … … 109 99 public function is_redirect() { 110 100 $code = $this->status_code; 111 return in_array($code, [300, 301, 302, 303, 307], true) || $code > 307 && $code < 400;101 return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400; 112 102 } 113 103 … … 115 105 * Throws an exception if the request was not successful 116 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}) 117 109 * @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})121 110 */ 122 111 public function throw_for_status($allow_redirects = true) { 123 112 if ($this->is_redirect()) { 124 if ( $allow_redirects !== true) {125 throw new Exception('Redirection not allowed', 'response.no_redirects', $this);113 if (!$allow_redirects) { 114 throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this); 126 115 } 127 116 } 128 117 elseif (!$this->success) { 129 $exception = Http::get_class($this->status_code);118 $exception = Requests_Exception_HTTP::get_class($this->status_code); 130 119 throw new $exception(null, $this); 131 120 } 132 121 } 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-decode140 *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 arrays144 * 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 array153 *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 }166 122 }
Note: See TracChangeset
for help on using the changeset viewer.