juliangut/body-parser
PSR7 请求体解析中间件
1.1
2017-10-16 21:43 UTC
Requires
- php: >=5.6
- psr/http-message: ^1.0
- willdurand/negotiation: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.0
- humbug/humbug: ~1.0@dev
- league/csv: ^8.0
- phpmd/phpmd: ^2.0
- phpunit/phpunit: ^4.5|^5.0
- sebastian/phpcpd: ^2.0
- squizlabs/php_codesniffer: ^2.0
- zendframework/zend-diactoros: ^1.0
Suggests
- league/csv: Used by CSV decoder
This package is auto-updated.
Last update: 2024-08-24 21:17:12 UTC
README
body-parser
PSR7 请求体解析中间件。
PSR7 实现通常不会解析请求体以通过 $request->getParsedBody()
使用,或者它们只为某些请求方法或内容类型执行此操作。
要完全确信在使用您想要的 PSR7 实现时请求内容将被正确解析,最好的方法是使用负责此任务的中间件。
安装
Composer
composer require juliangut/body-parser
用法
根据请求的 Content-Type
头添加尽可能多的内容解码器以满足您的应用程序需求。
解码器分配给一个或多个 HTTP 方法。
集成到您的中间件感知应用程序工作流程中。
require './vendor/autoload.php'; use Jgut\BodyParser\Decoder\Json; use Jgut\BodyParser\Decoder\Urlencoded; use Jgut\BodyParser\Parser; use Negotiator\Negtiator; $bodyParser = new Parser(new Negotiator()); $bodyParser->addDecoder(new Urlencoded()); // Assigned to all requests $bodyParser->addDecoder(new Json(), ['POST', 'PUT']); // Assigned only to POST and PUT requests $app = new \YourMiddlewareAwareApplication(); $app->addMiddleware($bodyParser); $app->run();
请查阅您使用的 PSR7 实现的文档,因为它可能在某些情况下已经解析了请求体。您不希望做同样的工作两次。
解码器
URL 编码
$decoder = new \Jgut\BodyParser\Decoder\UrlEncoded();
支持的 MIME 类型
- application/x-www-form-urlencoded
JSON
$decoder = new \Jgut\BodyParser\Decoder\Json();
支持的 MIME 类型
- application/json
- text/json
- application/x-json
XML
$decoder = new \Jgut\BodyParser\Decoder\Xml();
支持的 MIME 类型
- application/xml
- text/xml
- application/x-xml
CSV
$decoder = new \Jgut\BodyParser\Decoder\Csv($delimiter = ',', $enclosure = '"', $escape = '\\');
支持的 MIME 类型
- text/csv
自定义
您可以通过实现 Jgut\BodyParser\Decoder\Decoder
接口来创建自己的解码器。
例如,您可以为 application/x-yaml
和 text/yaml
MIME 类型实现 YAML 解码器。
贡献
发现了错误或有一个功能请求?请新建一个问题。在新建问题之前,请先查看现有的问题。
参见 CONTRIBUTING.md 文件
许可证
有关许可证条款的副本,请参阅源代码中包含的 LICENSE 文件