middlewares / payload
中间件,用于解析请求体,支持json、csv和url-encode
Requires
- php: ^7.2 || ^8.0
- middlewares/utils: ^3.0 || ^4.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
Suggests
- middlewares/csv-payload: Adds support for parsing CSV body of request
README
如果请求体未解析且方法为POST、PUT或DELETE,则解析请求体。它包含以下组件以支持不同格式
解析请求体失败将抛出 Middlewares\Utils\HttpErrorException
。有关更多信息,请参阅 middlewares/utils。
要求
- PHP >= 7.2
- PSR-7 http 库
- PSR-15 中间件调度器
安装
此包可通过Composer以 middlewares/payload 的方式安装和自动加载。
composer require middlewares/payload
JsonPayload
解析请求的JSON有效载荷。
Dispatcher::run([ (new Middlewares\JsonPayload()) ->associative(false) ->depth(64) ]); $response = $dispatcher->dispatch(new ServerRequest());
包含以下选项以配置 json_decode 函数
associative
默认启用,将对象转换为关联数组。
//Disable associative arrays $payload = (new Middlewares\JsonPayload())->associative(false);
depth
配置 json_decode 的递归深度选项。默认为 512
。
options
传递 json_decode 选项的掩码:JSON_BIGINT_AS_STRING
(默认启用),JSON_OBJECT_AS_ARRAY
,JSON_THROW_ON_ERROR
。
methods
配置允许的方法。默认情况下,只有方法为 POST, PUT, PATCH, DELETE, COPY, LOCK, UNLOCK
的请求被处理。
//Parse json only with POST and PUT requests $payload = (new Middlewares\JsonPayload())->methods(['POST', 'PUT']);
contentType
配置请求中允许的所有 Content-Type
标头。默认为 application/json
//Parse json only in request with these two Content-Type values $payload = (new Middlewares\JsonPayload())->contentType(['application/json', 'text/json']);
override
如果存在,则覆盖之前解析的请求体(默认为 false
)
UrlEncodePayload
解析请求的url-encoded有效载荷。
Dispatcher::run([ new Middlewares\UrlEncodePayload() ]);
methods
配置允许的方法。默认情况下,只有方法为 POST, PUT, PATCH, DELETE, COPY, LOCK, UNLOCK
的请求被处理。
contentType
配置请求中允许的所有 Content-Type 标头。默认为 application/x-www-form-urlencoded
override
如果存在,则覆盖之前解析的请求体(默认为 false
)
CsvPayload
CSV有效载荷由 middlewares/csv-payload 包支持。
XmlPayload
解析请求的XML有效载荷。解析后的请求体将返回一个 SimpleXMLElement 实例。
methods
配置允许的方法。默认情况下,只有方法为 POST, PUT, PATCH, DELETE, COPY, LOCK, UNLOCK
的请求被处理。
contentType
配置请求中允许的所有 Content-Type 标头。默认为 text/xml
,application/xml
和 application/x-xml
。
override
如果存在,则覆盖之前解析的请求体(默认为 false
)
有关最近更改的更多信息,请参阅 CHANGELOG,有关贡献的详细信息,请参阅 CONTRIBUTING。
MIT许可证(MIT)。有关更多信息,请参阅 LICENSE。