PHPackages                             dmstr/yii2-rest-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. dmstr/yii2-rest-sdk

ActiveYii2-extension[HTTP &amp; Networking](/categories/http)

dmstr/yii2-rest-sdk
===================

Yii2 extension providing typed entities, attribute mapping, and resource abstraction for consuming REST APIs via Guzzle

1.0.0(1mo ago)015—0%BSD-3-ClausePHPPHP &gt;=8.2CI passing

Since Mar 19Pushed 1mo agoCompare

[ Source](https://github.com/dmstr/yii2-rest-sdk)[ Packagist](https://packagist.org/packages/dmstr/yii2-rest-sdk)[ RSS](/packages/dmstr-yii2-rest-sdk/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Yii2 REST SDK
=============

[](#yii2-rest-sdk)

Typed entities, attribute mapping and resource abstraction for consuming REST APIs in Yii2 applications.

Installation
------------

[](#installation)

Install the package via composer

```
composer require dmstr/yii2-rest-sdk
```

Requires PHP 8.2 or higher.

Overview
--------

[](#overview)

The SDK provides a layered architecture for working with REST APIs:

- **HttpClient** handles authentication, caching and error handling on top of Guzzle
- **Resources** wrap API endpoints and provide a clean interface for CRUD operations
- **Entities** map API responses to typed PHP properties using PHP 8 attributes
- **Traits** for syncing entities with Yii2 ActiveRecord models

Configuration
-------------

[](#configuration)

Register your HttpClient implementation as a Yii2 application component with a `tokenProvider`. The provider can be an `AccessTokenProviderInterface` instance or a callable that returns one.

### With a callable (e.g. OpenID Connect / Keycloak)

[](#with-a-callable-eg-openid-connect--keycloak)

```
HttpClientInterface::class => [
    'class' => app\components\BlogClient::class,
    'baseUri' => getenv('API_BASE_URI'),
    'tokenProvider' => function () {
        $client = Yii::$app->authClientCollection->getClient('keycloak');
        return Yii::createObject([
            'class' => \dmstr\rest\sdk\auth\TokenProvider::class,
            'token' => $client->getAccessToken()?->getToken(),
        ]);
    },
    'timeout' => 30,
],
```

### With a static token

[](#with-a-static-token)

```
'tokenProvider' => [
    'class' => \dmstr\rest\sdk\auth\TokenProvider::class,
    'token' => 'my-api-key',
],
```

### Without authentication

[](#without-authentication)

Omit `tokenProvider` entirely — requests will be sent without an Authorization header.

Usage
-----

[](#usage)

### Defining an HttpClient

[](#defining-an-httpclient)

Extend the abstract `HttpClient` and add methods that return your resources.

```
