httpsoft / http-response
PSR-7 Response implementations
Installs: 29 729
Dependents: 14
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^7.4|^8.0
- httpsoft/http-message: ^1.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^4.9|^5.2
Provides
README
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'