chris-doehring/enm-json-api-client

此包已被废弃且不再维护。作者建议使用dogado/json-api-client包代替。

JSON API规范(jsonapi.org)的客户端PHP实现

6.1.0 2020-10-31 22:36 UTC

This package is auto-updated.

Last update: 2021-06-14 20:02:38 UTC


README

Build Status Coverage Status Total Downloads Latest Stable Version Latest Unstable Version License

此包已被废弃。请使用dogado/json-api-client代替。

JSON API规范(jsonapi.org)的客户端PHP实现。

它基于原始创作,由eosnewmedia团队和维护者Philipp Marien完成。

安装

composer require chris-doehring/enm-json-api-client

建议安装http-client的guzzlehttp/guzzle版本^7.0,以及用于PSR-17兼容工厂的http-interop/http-factory-guzzle

composer require guzzlehttp/guzzle http-interop/http-factory-guzzle

您也可以使用任何其他实现了PSR-18的HTTP客户端。

使用方法

首先,您应该阅读chris-doehring/enm-json-api-common中的文档,其中定义了所有基本结构。

您的API客户端是Enm\JsonApi\Client\JsonApiClient的一个实例,它需要一个PSR-18 HTTP客户端(Psr\Http\Client\ClientInterface)来执行请求。

$client = new JsonApiClient(
    'http://example.com/api',
    $httpClient, // instance of Psr\Http\Client\ClientInterface
    $uriFactory, // instance of Psr\Http\Message\UriFactoryInterface
    $requestFactory, // instance of Psr\Http\Message\RequestFactoryInterface
    $streamFactory, // instance of Psr\Http\Message\StreamFactoryInterface
    new Serializer(),
    new Deserializer()
);

$request = $client->createGetRequest(new Uri('/myResources/1')); // will fetch the resource at http://example.com/api/myResources/1
$request->requestInclude('myRelationship'); // include a relationship

$response = $client->execute($request);

$document = $response->document();
$myResource = $document->data()->first(); // the resource fetched by this request
$myIncludedResources = $document->included()->all(); // the included resources fetched with the include parameter