Response.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * @var float|boolean
  49. */
  50. public $protocol_version = false;
  51. /**
  52. * Whether the request succeeded or not
  53. *
  54. * @var boolean
  55. */
  56. public $success = false;
  57. /**
  58. * Number of redirects the request used
  59. *
  60. * @var integer
  61. */
  62. public $redirects = 0;
  63. /**
  64. * URL requested
  65. *
  66. * @var string
  67. */
  68. public $url = '';
  69. /**
  70. * Previous requests (from redirects)
  71. *
  72. * @var array Array of Requests_Response objects
  73. */
  74. public $history = array();
  75. /**
  76. * Cookies from the request
  77. *
  78. * @var Requests_Cookie_Jar Array-like object representing a cookie jar
  79. */
  80. public $cookies = array();
  81. /**
  82. * Is the response a redirect?
  83. *
  84. * @return boolean True if redirect (3xx status), false if not.
  85. */
  86. public function is_redirect() {
  87. $code = $this->status_code;
  88. return in_array($code, array(300, 301, 302, 303, 307)) || $code > 307 && $code < 400;
  89. }
  90. /**
  91. * Throws an exception if the request was not successful
  92. *
  93. * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
  94. * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})
  95. * @param boolean $allow_redirects Set to false to throw on a 3xx as well
  96. */
  97. public function throw_for_status($allow_redirects = true) {
  98. if ($this->is_redirect()) {
  99. if (!$allow_redirects) {
  100. throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);
  101. }
  102. }
  103. elseif (!$this->success) {
  104. $exception = Requests_Exception_HTTP::get_class($this->status_code);
  105. throw new $exception(null, $this);
  106. }
  107. }
  108. }