# PHP package for managing HTTP responses
The HttpSoft\Response package implements Psr\Http\Message\ResponseInterface in accordance with the RFC 7230 and RFC 7231 specifications.
Depends on the HttpSoft\Message package.
This package requires PHP version 7.4 or later.
Package installation:
composer require httpsoft/http-response
# API
Psr\Http\Message\ResponseInterface and HttpSoft\Response\ResponseStatusCodeInterface Implementations:
- HttpSoft\Response\EmptyResponse.
- HttpSoft\Response\HtmlResponse.
- HttpSoft\Response\JsonResponse.
- HttpSoft\Response\RedirectResponse.
- HttpSoft\Response\TextResponse.
- HttpSoft\Response\XmlResponse.
# Usage
// 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'
Usage standard response: HttpSoft\Message\Response.