Response.php 4.1 KB

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