|
1 | 1 | # HTTP Response |
2 | 2 |
|
3 | | -The repository is under development, and the description will appear later. |
| 3 | +[](https://packagist.org/packages/httpsoft/http-response) |
| 4 | +[](https://packagist.org/packages/httpsoft/http-response) |
| 5 | +[](https://packagist.org/packages/httpsoft/http-response) |
| 6 | +[](https://github.com/httpsoft/http-response/actions) |
| 7 | +[](https://scrutinizer-ci.com/g/httpsoft/http-response/?branch=master) |
| 8 | +[](https://scrutinizer-ci.com/g/httpsoft/http-response/?branch=master) |
| 9 | + |
| 10 | +This package contains a collection of classes that implements [Psr\Http\Message\ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php) from [PSR-7 HTTP Message](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md) in accordance with the [RFC 7230](https://tools.ietf.org/html/rfc7230) and [RFC 7231](https://tools.ietf.org/html/rfc7231) specifications. |
| 11 | + |
| 12 | +## Documentation |
| 13 | + |
| 14 | +* [In English language](https://httpsoft.org/docs/response). |
| 15 | +* [In Russian language](https://httpsoft.org/ru/docs/response). |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +This package requires PHP version 7.4 or later. |
| 20 | + |
| 21 | +``` |
| 22 | +composer require httpsoft/http-response |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage Response |
| 26 | + |
| 27 | +```php |
| 28 | +use HttpSoft\Response\Response; |
| 29 | +use HttpSoft\Response\ResponseFactory; |
| 30 | + |
| 31 | +$response = ResponseFactory::create(); |
| 32 | +// equivalently to: |
| 33 | +$response = new Response(); |
| 34 | +// default values |
| 35 | +$response->getStatusCode(); // 200 |
| 36 | +$response->getReasonPhrase(); // 'OK' |
| 37 | +$response->getBody()->getContents(); // '' |
| 38 | +$response->getBody()->getMetadata('uri') // 'php://temp' |
| 39 | +$response->getHeaders(); // [] |
| 40 | +$response->getProtocolVersion(); // '1.1' |
| 41 | + |
| 42 | +// Create with the passed parameters |
| 43 | +$response = new Response(404, '', 'php://memory', ['Content-Language' => 'en'], '2'); |
| 44 | +$response->getStatusCode(); // 404 |
| 45 | +$response->getReasonPhrase(); // 'Not Found' |
| 46 | +$response->getBody()->getContents(); // '' |
| 47 | +$response->getBody()->getMetadata('uri') // 'php://memory' |
| 48 | +$response->getHeaders(); // ['Content-Language' => ['en']] |
| 49 | +$response->getProtocolVersion(); // '2' |
| 50 | + |
| 51 | +// Write to the response body: |
| 52 | +$response->getBody()->write('Content'); |
| 53 | +$response->getBody()->getContents(); // 'Content' |
| 54 | + |
| 55 | +// With `Content-Type` header: |
| 56 | +$newResponse = $response->withHeader('Content-Type', 'text/plain'); |
| 57 | +$newResponse->getHeaderLine('content-type'); // 'text/plain' |
| 58 | +$newResponse->getHeaders(); // ['Content-Language' => ['ru'], 'Content-Type' => ['text/plain']] |
| 59 | + |
| 60 | +// With status code: |
| 61 | +$newResponse = $response->withStatus(Response::STATUS_INTERNAL_SERVER_ERROR); |
| 62 | +$newResponse->getStatusCode(); // 500 |
| 63 | +$newResponse->getReasonPhrase(); // 'Internal Server Error' |
| 64 | + |
| 65 | +// With status code and reason phrase: |
| 66 | +$newResponse = $response->withStatus(599, 'Custom Phrase'); |
| 67 | +$newResponse->getStatusCode(); // 599 |
| 68 | +$newResponse->getReasonPhrase(); // 'Custom Phrase' |
| 69 | +``` |
| 70 | + |
| 71 | +## Create custom responses |
| 72 | + |
| 73 | +```php |
| 74 | +// Create `Psr\Http\Message\ResponseInterface` instance from HTML: |
| 75 | +$response = new HttpSoft\Response\HtmlResponse('<p>HTML</p>'); |
| 76 | +$response->getHeaderLine('content-type'); // 'text/html; charset=UTF-8' |
| 77 | + |
| 78 | +// Create `Psr\Http\Message\ResponseInterface` instance from data to convert to JSON: |
| 79 | +$response = new HttpSoft\Response\JsonResponse(['key' => 'value']); |
| 80 | +$response->getHeaderLine('content-type'); // 'application/json; charset=UTF-8' |
| 81 | + |
| 82 | +// Create `Psr\Http\Message\ResponseInterface` instance from Text: |
| 83 | +$response = new HttpSoft\Response\TextResponse('Text'); |
| 84 | +$response->getHeaderLine('content-type'); // 'text/plain; charset=UTF-8' |
| 85 | + |
| 86 | +// Create `Psr\Http\Message\ResponseInterface` instance from XML: |
| 87 | +$response = new HttpSoft\Response\XmlResponse('<xmltag>XML</xmltag>'); |
| 88 | +$response->getHeaderLine('content-type'); // 'application/xml; charset=UTF-8' |
| 89 | + |
| 90 | +// Create `Psr\Http\Message\ResponseInterface` instance for redirect: |
| 91 | +$response = new HttpSoft\Response\RedirectResponse('https/example.com'); |
| 92 | +$response->getHeaderLine('location'); // 'https/example.com' |
| 93 | + |
| 94 | +// Create `Psr\Http\Message\ResponseInterface` instance for empty response: |
| 95 | +$response = new HttpSoft\Response\EmptyResponse(); |
| 96 | +$response->getStatusCode(); // 204 |
| 97 | +$response->getReasonPhrase(); // 'No Content' |
| 98 | +``` |
0 commit comments