3
namespace GuzzleHttp\Message;
5
use GuzzleHttp\Exception\ParseException;
6
use GuzzleHttp\Exception\XmlParseException;
7
use GuzzleHttp\Stream\StreamInterface;
10
* Guzzle HTTP response object
12
class Response extends AbstractMessage implements ResponseInterface
14
/** @var array Mapping of status codes to reason phrases */
15
private static $statusTexts = [
17
101 => 'Switching Protocols',
22
203 => 'Non-Authoritative Information',
24
205 => 'Reset Content',
25
206 => 'Partial Content',
26
207 => 'Multi-Status',
27
208 => 'Already Reported',
29
300 => 'Multiple Choices',
30
301 => 'Moved Permanently',
33
304 => 'Not Modified',
35
307 => 'Temporary Redirect',
36
308 => 'Permanent Redirect',
38
401 => 'Unauthorized',
39
402 => 'Payment Required',
42
405 => 'Method Not Allowed',
43
406 => 'Not Acceptable',
44
407 => 'Proxy Authentication Required',
45
408 => 'Request Timeout',
48
411 => 'Length Required',
49
412 => 'Precondition Failed',
50
413 => 'Request Entity Too Large',
51
414 => 'Request-URI Too Long',
52
415 => 'Unsupported Media Type',
53
416 => 'Requested Range Not Satisfiable',
54
417 => 'Expectation Failed',
55
422 => 'Unprocessable Entity',
57
424 => 'Failed Dependency',
58
425 => 'Reserved for WebDAV advanced collections expired proposal',
59
426 => 'Upgrade required',
60
428 => 'Precondition Required',
61
429 => 'Too Many Requests',
62
431 => 'Request Header Fields Too Large',
63
500 => 'Internal Server Error',
64
501 => 'Not Implemented',
66
503 => 'Service Unavailable',
67
504 => 'Gateway Timeout',
68
505 => 'HTTP Version Not Supported',
69
506 => 'Variant Also Negotiates (Experimental)',
70
507 => 'Insufficient Storage',
71
508 => 'Loop Detected',
72
510 => 'Not Extended',
73
511 => 'Network Authentication Required',
76
/** @var string The reason phrase of the response (human readable code) */
77
private $reasonPhrase;
79
/** @var string The status code of the response */
82
/** @var string The effective URL that returned this response */
83
private $effectiveUrl;
86
* @param string $statusCode The response status code (e.g. 200)
87
* @param array $headers The response headers
88
* @param StreamInterface $body The body of the response
89
* @param array $options Response message options
90
* - reason_phrase: Set a custom reason phrase
91
* - protocol_version: Set a custom protocol version
93
public function __construct(
96
StreamInterface $body = null,
99
$this->statusCode = (string) $statusCode;
100
$this->handleOptions($options);
102
// Assume a reason phrase if one was not applied as an option
103
if (!$this->reasonPhrase &&
104
isset(self::$statusTexts[$this->statusCode])
106
$this->reasonPhrase = self::$statusTexts[$this->statusCode];
110
$this->setHeaders($headers);
114
$this->setBody($body);
118
public function getStatusCode()
120
return $this->statusCode;
123
public function getReasonPhrase()
125
return $this->reasonPhrase;
128
public function json(array $config = [])
131
return \GuzzleHttp\json_decode(
132
(string) $this->getBody(),
133
isset($config['object']) ? !$config['object'] : true,
135
isset($config['big_int_strings']) ? JSON_BIGINT_AS_STRING : 0
137
} catch (\InvalidArgumentException $e) {
138
throw new ParseException(
145
public function xml(array $config = [])
147
$disableEntities = libxml_disable_entity_loader(true);
148
$internalErrors = libxml_use_internal_errors(true);
151
// Allow XML to be retrieved even if there is no response body
152
$xml = new \SimpleXMLElement(
153
(string) $this->getBody() ?: '<root />',
154
isset($config['libxml_options']) ? $config['libxml_options'] : LIBXML_NONET,
156
isset($config['ns']) ? $config['ns'] : '',
157
isset($config['ns_is_prefix']) ? $config['ns_is_prefix'] : false
159
libxml_disable_entity_loader($disableEntities);
160
libxml_use_internal_errors($internalErrors);
161
} catch (\Exception $e) {
162
libxml_disable_entity_loader($disableEntities);
163
libxml_use_internal_errors($internalErrors);
164
throw new XmlParseException(
165
'Unable to parse response body into XML: ' . $e->getMessage(),
168
(libxml_get_last_error()) ?: null
175
public function getEffectiveUrl()
177
return $this->effectiveUrl;
180
public function setEffectiveUrl($url)
182
$this->effectiveUrl = $url;
188
* Accepts and modifies the options provided to the response in the
191
* @param array $options Options array passed by reference.
193
protected function handleOptions(array &$options = [])
195
parent::handleOptions($options);
196
if (isset($options['reason_phrase'])) {
197
$this->reasonPhrase = $options['reason_phrase'];