krzysztof-moskalik / api-client
Client for consuming REST APIs
Requires
- php: >=8.4
- guzzlehttp/psr7: ^2.8
- phpdocumentor/reflection-docblock: ^6.0
- psr/http-client: ^1.0
- psr/http-message: ^2.0
- symfony/property-access: ^8.0
- symfony/serializer: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.92
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.5
- vimeo/psalm: ^6.14
Suggests
- guzzlehttp/guzzle: ^7.10
This package is auto-updated.
Last update: 2026-03-01 00:48:55 UTC
README
This library makes an abstraction layer for REST APIs.
It allows mapping API resources to your application models.
Note
This library is under development!
Many things can be changed, and many new features can be added.
Symfony Note
This library is not tied to a Symfony framework, but there is a Symfony bundle available: KrzysztofMoskalik/api-client-bundle.
Features:
- Easy to use
- Flexible
- Easy to extend
- Works the best with standard JSON REST APIs
- Supports multiple APIs
- Supports multiple authentication methods
- Supports multiple endpoints per same resource (with the same HTTP method)
Simple configuration
Api Client does not come with a builtin serializer, so first you need to define your own one.
For example:
use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; $serializer = new Serializer( [ new ObjectNormalizer(), new ArrayDenormalizer(), ], [ new JsonEncoder(), ] );
Same thing with HTTP Client:
$httpClient = new GuzzleHttp\Client();
Finally, you can create an Api Client instance with a simple configuration:
use App\Model\User; use KrzysztofMazur\Api\Api; use KrzysztofMazur\Api\Client; $client = new Client(); $client->globals ->setSerializer($serializer) ->setHttpClient($httpClient) $client->addApi( new Api('my_api_name') ->setBaseUrl('https://my-api.com/api/') ->generic(User::class) );
This will create repository for all CRUD operations on User resource that will be represented by App\Model\User class.
Usage
With this simple configuration you can now use Api Client to interact with your API:
$repository = $client->getRepository(User::class); $user = $repository->get(2340); // fetch user with id 2340 $users = $repository->list(); // fetch all users $repository->delete(3671); // delete user with id 3671 $user = new User( 'John Doe', 'john.doe@example.com' ); $repository->create($user); // create new user $user->setEmail('john.doe@lorem.com'); $repository->update($user); // update user $user->setEmail('john.doe@ipsum.com'); $repository->patch($user); // partial update user
Full config reference
Work in progress...
Todos
- handling filtering and sorting on list operations
- handling nesting data paths
- implement bearer auth