httpsoft/http-response

PSR-7 响应实现

1.1.0 2023-05-05 20:55 UTC

This package is auto-updated.

Last update: 2024-09-06 00:03:13 UTC


README

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

本包包含一组类,实现了根据Psr\Http\Message\ResponseInterfacePSR-7 HTTP Message规范,以及RFC 7230RFC 7231规范编写的Psr\Http\Message\ResponseInterface

依赖于httpsoft/http-message包。

文档

安装

此包需要 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'