chris-doehring / enm-json-api-client
6.1.0
2020-10-31 22:36 UTC
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- chris-doehring/enm-json-api-common: ^5.1
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
Requires (Dev)
- fakerphp/faker: ^1.10
- guzzlehttp/guzzle: ^7.0
- http-interop/http-factory-guzzle: ^1.0
- phpunit/phpunit: ^9.0
Suggests
- guzzlehttp/guzzle: A PSR 7 and 18 compatible, easy to use, PHP HTTP client
- http-interop/http-factory-guzzle: PSR 17 compatible guzzle factory library package
README
此包已被废弃。请使用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