httpsoft / http-response
PSR-7 响应实现
1.1.0
2023-05-05 20:55 UTC
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
本包包含一组类,实现了根据Psr\Http\Message\ResponseInterface和PSR-7 HTTP Message规范,以及RFC 7230和RFC 7231规范编写的Psr\Http\Message\ResponseInterface。
文档
安装
此包需要 PHP 版本 7.4 或更高。
composer require httpsoft/http-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'
使用自定义响应
// 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'