PHP client to connect to the Unomi API.
The package is available via composer:
$ composer require dropsolid/unomi-sdk-php
This package requires an HTTP client supported by PHP-HTTP.
A default implementation has been provided if you use Guzzle
(guzzlehttp/guzzle
).
use Dropsolid\UnomiSdkPhp\Http\Guzzle\GuzzleApiClientFactory;
$apiClient = GuzzleApiClientFactory::createBasicAuth(
[
'timeout' => 3.0,
'base_uri' => 'localhost',
'auth' => ['karaf', 'karaf']
]
);
If you want to work with deserialized models, you can make use of the
Unomi
class. This acts as a wrapper for the API. You'll need
to install Symfony's Serializer component
(symfony/serializer
) for this.
Instantiate Unomi as shown below to make use of the repositories
<?php
use Dropsolid\UnomiSdkPhp\Unomi;
$unomi = Unomi::withDefaultSerializer($apiClient);
// Performing a segment.list request with offset
$offset = [
'offset' => 0,
];
// Get all itemIds & conditions from all existing segments
$segments = $unomi->segments()->listSegments($offset);
foreach ($segments as $segmentMetadata) {
$id = $segmentMetadata->getId();
// Performing a segment.info request
$segment = $unomi->segments()->getSegment($id);
var_dump($segment->getItemId());
var_dump($segment->getCondition());
}
$unomi->segments()->getSegments($id); // Get segemnt as per id
$unomi->segments()->listSegments(); // List all available segments
$unomi->rules()->listRules(); // List all available rules
$unomi->rules->listRulesStatistics(); // List statistics about the rules
$unomi->profile()->getProfile($id); // Get profile as per id
$unomi->profile()->getProfileSegments($id); // Get profile segments as per id
$unomi->profile()->listProfile(); // List all available profiles
$unomi->profile()->listProperties(); // List properties of profiles
$unomi->events()->listEventsUsingEventType($eventType); // Get events as per event type
$unomi->events()->listEventsUsingProfileId($profileId); // Get events as per profile id
$unomi->definitions()->listActions(); // Get all available action types
$unomi->definitions()->listConditions(); // Get all available conditions types
$unomi->definitions()->listValues(); // Get all available values types
The Unomi API could have a middleware with OAuth2.0 in front or use simple basic auth. The API allows to use both and for the OAuth2 implementation we rely on The PHP league's OAuth2 client. This was optimized for use with the third-party Dropsolid Platform OAuth 2.0 provider to handle this.
<?php
use Dropsolid\UnomiSdkPhp\Http\Guzzle\GuzzleApiClientFactory;
use Dropsolid\UnomiSdkPhp\Unomi;
$apiClient = GuzzleApiClientFactory::createBasicAuth(
[
'timeout' => 3.0,
'base_uri' => 'localhost',
'auth' => ['karaf', 'karaf']
]
);
$unomi = Unomi::withDefaultSerializer($apiClient);
<?php
use Dropsolid\UnomiSdkPhp\Http\Guzzle\GuzzleApiClientFactory;
use Dropsolid\OAuth2\Client\Provider\DropsolidPlatform;
$provider = new DropsolidPlatform(
[
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
'urlAuthorize' => 'https://admin.platform.dropsolid.com/oauth/authorize',
'urlAccessToken' => 'https://admin.platform.dropsolid.com/oauth/token',
'urlResourceOwnerDetails' => 'https://admin.platform.dropsolid.com/oauth/user.info',
'scopes' => 'cdp_admin',
]
);
$accessToken = $provider->getAccessToken('client_credentials');
$apiClient = GuzzleApiClientFactory::createOauth(
$provider,
$accessToken,
['timeout' => 3.0, 'base_uri' => 'https://unomi.poc.qa.dropsolid-sites.com']
);
<?php
use Dropsolid\UnomiSdkPhp\Http\ApiClient\ApiClient;
use GuzzleHttp\Client as GuzzleHttpClient;
use Http\Adapter\Guzzle6\Client;
$config = [ // Optional extra configuration.
'timeout' => 3.0,
'base_uri' => 'https://unomi.poc.qa.dropsolid-sites.com'
];
// Create a PHP-HTTP compatible client.
$httpClient = new GuzzleHttpClient($config);
$psrClient = new Client($httpClient);
// Alternatively, you can use any other PHP-HTTP compatible client of your
// choosing to instantiate the API client.
$apiClient = new ApiClient(
$provider,
$psrClient,
null
);