httpsoft/http-response

PSR-7 Response implementations

1.1.0 2023-05-05 20:55 UTC

This package is auto-updated.

Last update: 2024-10-06 00:12:18 UTC


README

License Latest Stable Version Total Downloads GitHub Build Status GitHub Static Analysis Status Scrutinizer Code Coverage Scrutinizer Code Quality

This package contains a collection of classes that implements Psr\Http\Message\ResponseInterface from PSR-7 HTTP Message in accordance with the RFC 7230 and RFC 7231 specifications.

Depends on the httpsoft/http-message package.

Documentation

Installation

This package requires PHP version 7.4 or later.

composer require httpsoft/http-response

Usage standard response

use HttpSoft\Message\Response;

$response = new Response();
// default values
$response->getStatusCode(); // 200
$response->getReasonPhrase(); // 'OK'
$response->getBody()->getContents(); // ''
$response->getBody()->getMetadata('uri') // 'php://temp'
$response->getHeaders(); // []
$response->getProtocolVersion(); // '1.1'

// Create with the passed parameters
$response = new Response(404, ['Content-Language' => 'en'], 'php://memory', '2');
$response->getStatusCode(); // 404
$response->getReasonPhrase(); // 'Not Found'
$response->getBody()->getContents(); // ''
$response->getBody()->getMetadata('uri') // 'php://memory'
$response->getHeaders(); // ['Content-Language' => ['en']]
$response->getProtocolVersion(); // '2'

// Write to the response body:
$response->getBody()->write('Content');
$response->getBody()->getContents(); // 'Content'

// With `Content-Type` header:
$newResponse = $response->withHeader('Content-Type', 'text/plain');
$newResponse->getHeaderLine('content-type'); // 'text/plain'
$newResponse->getHeaders(); // ['Content-Language' => ['ru'], 'Content-Type' => ['text/plain']]

// With status code:
$newResponse = $response->withStatus(500);
$newResponse->getStatusCode(); // 500
$newResponse->getReasonPhrase(); // 'Internal Server Error'

// With status code and reason phrase:
$newResponse = $response->withStatus(599, 'Custom Phrase');
$newResponse->getStatusCode(); // 599
$newResponse->getReasonPhrase(); // 'Custom Phrase'

Usage custom responses

// Create `Psr\Http\Message\ResponseInterface` instance from HTML: 
$response = new HttpSoft\Response\HtmlResponse('<p>HTML</p>');
$response->getHeaderLine('content-type'); // 'text/html; charset=UTF-8'

// Create `Psr\Http\Message\ResponseInterface` instance from data to convert to JSON: 
$response = new HttpSoft\Response\JsonResponse(['key' => 'value']);
$response->getHeaderLine('content-type'); // 'application/json; charset=UTF-8'

// Create `Psr\Http\Message\ResponseInterface` instance from Text: 
$response = new HttpSoft\Response\TextResponse('Text');
$response->getHeaderLine('content-type'); // 'text/plain; charset=UTF-8'

// Create `Psr\Http\Message\ResponseInterface` instance from XML: 
$response = new HttpSoft\Response\XmlResponse('<xmltag>XML</xmltag>');
$response->getHeaderLine('content-type'); // 'application/xml; charset=UTF-8'

// Create `Psr\Http\Message\ResponseInterface` instance for redirect: 
$response = new HttpSoft\Response\RedirectResponse('https/example.com');
$response->getHeaderLine('location'); // 'https/example.com'

// Create `Psr\Http\Message\ResponseInterface` instance for empty response: 
$response = new HttpSoft\Response\EmptyResponse();
$response->getStatusCode(); // 204
$response->getReasonPhrase(); // 'No Content'