openpublicmedia/engaging-networks-php

PHP client for Engaging Networks Services (ENS) APIs

0.10.0 2023-08-28 20:30 UTC

This package is auto-updated.

Last update: 2024-10-28 18:00:40 UTC


README

This library abstracts interactions with the Engaging Networks Services (ENS) APIs.

Implemented APIs:

Installation

Install via composer:

composer require openpublicmedia/engaging-networks-php

Use

REST API

The OpenPublicMedia\EngagingNetworksServices\Rest\Client queries the REST API. The client requires an API User with a whitelisted IP address and an API token in the target Engaging Networks account.

See REST API Instructions

Examples

Creating a client

use OpenPublicMedia\EngagingNetworksServices\Rest\Client;

$base_uri = 'https://ca.engagingnetworks.app/ens/service/';
$api_key = '11111111-2222-3333-4444-555555555555';

$client = new Client($base_uri, $api_key);

Providing a cache service is also supported (and recommended) when creating the client. If the client has a cache service it will be used to cache the authentication token provided by the ENS REST API across multiple requests for the lifetime of the token.

A PSR-16 compliant interface is preferred but any class providing set($key, $value) and get($key, $default) methods will suffice.

use OpenPublicMedia\EngagingNetworksServices\Rest\Client;
use Tochka\Cache\ArrayFileCache;

$base_uri = 'https://ca.engagingnetworks.app/ens/service/';
$api_key = '11111111-2222-3333-4444-555555555555';

$cache = new ArrayFileCache('.', 'my_awesome_cache');
$client = new Client($base_uri, $api_key, cache: $cache);

Note: ENS REST API tokens are associated with the IP address used to generate the token. The client provides default cache key names for the token and expiration data. In configurations where multiple backends with different IP addresses handle requests the cache_key_token and cache_key_token_expire parameters can be used to set the cache keys.

Server 1

use OpenPublicMedia\EngagingNetworksServices\Rest\Client;
use Tochka\Cache\ArrayFileCache;

$base_uri = 'https://ca.engagingnetworks.app/ens/service/';
$api_key = '11111111-2222-3333-4444-555555555555';

$cache = new ArrayFileCache('.', 'my_awesome_cache');
$client = new Client(
  $base_uri,
  $api_key,
  cache: $cache,
  cache_key_token: 'open_public_media.ens.rest.session_token_server_1',
  cache_key_token_expire: 'open_public_media.ens.rest.session_expire_server_1'
);

Server 2

use OpenPublicMedia\EngagingNetworksServices\Rest\Client;
use Tochka\Cache\ArrayFileCache;

$base_uri = 'https://ca.engagingnetworks.app/ens/service/';
$api_key = '11111111-2222-3333-4444-555555555555';

$cache = new ArrayFileCache('.', 'my_awesome_cache');
$client = new Client(
  $base_uri,
  $api_key,
  cache: $cache,
  cache_key_token: 'open_public_media.ens.rest.session_token_server_2',
  cache_key_token_expire: 'open_public_media.ens.rest.session_expire_server_2'
);

Handling exceptions

Custom exceptions are provided for 404 response and general errors. Additional information from the ENS REST API is captured in these exceptions.

use OpenPublicMedia\EngagingNetworksServices\Rest\Client;

$base_uri = 'https://ca.engagingnetworks.app/ens/service/';
$api_key = '11111111-2222-3333-4444-555555555555';

$client = new Client($base_uri, $api_key);
try {
    $results = $client->getPages(PageType::dc);
} catch (Exception $e) {
    var_dump(get_class($e));
    var_dump($e->getMessage());
    var_dump($e->getCode());
    var_dump($e->getErrorMessage());
    var_dump($e->getErrorMessageId());
}

Development goals

See CONTRIBUTING for information about contributing to this project.

v1