enm / json-api-client
Abstract client-side php implementation of the json api specification (jsonapi.org)
Installs: 30 850
Dependents: 1
Suggesters: 0
Security: 0
Stars: 18
Watchers: 7
Forks: 7
Open Issues: 2
Requires
- enm/json-api-common: ^3.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
Requires (Dev)
- nyholm/psr7: ^1.2
- phpunit/phpunit: ^8.3
This package is auto-updated.
Last update: 2024-11-22 00:12:30 UTC
README
Abstract client-side PHP implementation of the json api specification.
Installation
composer require enm/json-api-client
It's recommended to install kriswallsmith/buzz
as http-client and nyholm/psr7
for http factories.
composer require kriswallsmith/buzz nyholm/psr7
You can also use any HTTP client which implements PSR-18.
Usage
First you should read the docs at enm/json-api-common where all basic structures are defined.
Your API client is an instance of Enm\JsonApi\Client\JsonApiClient
, which requires a PSR-18 HTTP client (Psr\Http\Client\ClientInterface
) to execute requests.
$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