danharper/psr7-jsonx

JSONx 是以 XML 格式编码的 JSON

v0.1.0 2015-11-22 23:49 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:58:59 UTC


README

Latest Stable Version License

一个带有 Content-Type: application/xml 标头的进入请求对象,将被转换为 JSON 请求。

一个带有 Content-Type: application/json 标头的输出响应对象,与具有 Accept: application/xml 标头的相应请求对象,将被转换为 XML 响应。

它使用 IBM 的标准来表示 JSON 作为 XML:JSONx

这个库只是围绕 danharper/jsonx 的小封装。如果你使用 Laravel,只需添加一个中间件即可获得完整的 JSONx:danharper/laravel-jsonx

注意 该库会更改 HTTP 消息的主体,因此它需要一个实现 Psr\Http\Message\StreamInterface 的实现来工作。或者,我可能遗漏了更好的方法?

你可以实现一个 StreamFactoryInterface 来实现它,或者有两个实现:DiactorosStreamFactory(用于 zend/diactoros PSR-7 实现)和 CallableStreamFactory,你提供的一个函数将被给一个字符串,你必须返回包含它的流。

$jsonxAdaptor = new JSONxPSR7(new DiactorosStreamFactory);

// or instead of DiactorosStreamFactory, implement your own, or use:

$streamFactory = new CallableStreamFactory(function($body) { return MyPsr7Stream::fromString($body); });
$jsonxAdaptor = new JSONxPSR7($streamFactory);

安装

composer require danharper/psr7-jsonx

示例

将请求转换为如下

$request = $jsonxAdaptor->request($request);

将响应转换为如下

$response = $jsonxAdaptor->response($request, $response);