namespace GuzzleHttp\Psr7;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
* PSR-7 response implementation.
class Response implements ResponseInterface
/** @var array Map of standard HTTP status code/reason phrases */
private static $phrases = [
101 => 'Switching Protocols',
203 => 'Non-Authoritative Information',
206 => 'Partial Content',
208 => 'Already Reported',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
307 => 'Temporary Redirect',
402 => 'Payment Required',
405 => 'Method Not Allowed',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
422 => 'Unprocessable Entity',
424 => 'Failed Dependency',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
451 => 'Unavailable For Legal Reasons',
500 => 'Internal Server Error',
501 => 'Not Implemented',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
511 => 'Network Authentication Required',
private $reasonPhrase = '';
private $statusCode = 200;
* @param int $status Status code
* @param array $headers Response headers
* @param string|resource|StreamInterface|null $body Response body
* @param string $version Protocol version
* @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
public function __construct(
$this->assertStatusCodeIsInteger($status);
$this->assertStatusCodeRange($status);
$this->statusCode = $status;
if ($body !== '' && $body !== null) {
$this->stream = Utils::streamFor($body);
$this->setHeaders($headers);
if ($reason == '' && isset(self::$phrases[$this->statusCode])) {
$this->reasonPhrase = self::$phrases[$this->statusCode];
$this->reasonPhrase = (string) $reason;
$this->protocol = $version;
public function getStatusCode()
return $this->statusCode;
public function getReasonPhrase()
return $this->reasonPhrase;
public function withStatus($code, $reasonPhrase = '')
$this->assertStatusCodeIsInteger($code);
$this->assertStatusCodeRange($code);
$new->statusCode = $code;
if ($reasonPhrase == '' && isset(self::$phrases[$new->statusCode])) {
$reasonPhrase = self::$phrases[$new->statusCode];
$new->reasonPhrase = (string) $reasonPhrase;
private function assertStatusCodeIsInteger($statusCode)
if (filter_var($statusCode, FILTER_VALIDATE_INT) === false) {
throw new \InvalidArgumentException('Status code must be an integer value.');
private function assertStatusCodeRange($statusCode)
if ($statusCode < 100 || $statusCode >= 600) {
throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.');