截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Response.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * HTTP response class
  4. *
  5. * Contains a response from Requests::request()
  6. * @package Requests
  7. */
  8. /**
  9. * HTTP response class
  10. *
  11. * Contains a response from Requests::request()
  12. * @package Requests
  13. */
  14. class Requests_Response {
  15. /**
  16. * Constructor
  17. */
  18. public function __construct() {
  19. $this->headers = new Requests_Response_Headers();
  20. $this->cookies = new Requests_Cookie_Jar();
  21. }
  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 Requests_Response_Headers Array-like object representing headers
  38. */
  39. public $headers = array();
  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 Requests_Response objects
  74. */
  75. public $history = array();
  76. /**
  77. * Cookies from the request
  78. *
  79. * @var Requests_Cookie_Jar Array-like object representing a cookie jar
  80. */
  81. public $cookies = array();
  82. /**
  83. * Is the response a redirect?
  84. *
  85. * @return boolean True if redirect (3xx status), false if not.
  86. */
  87. public function is_redirect() {
  88. $code = $this->status_code;
  89. return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400;
  90. }
  91. /**
  92. * Throws an exception if the request was not successful
  93. *
  94. * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
  95. * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})
  96. * @param boolean $allow_redirects Set to false to throw on a 3xx as well
  97. */
  98. public function throw_for_status($allow_redirects = true) {
  99. if ($this->is_redirect()) {
  100. if (!$allow_redirects) {
  101. throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);
  102. }
  103. }
  104. elseif (!$this->success) {
  105. $exception = Requests_Exception_HTTP::get_class($this->status_code);
  106. throw new $exception(null, $this);
  107. }
  108. }
  109. }