PHP code example of eppo / php-sdk

1. Go to this page and download the library: Download eppo/php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eppo / php-sdk example snippets




use Eppo\EppoClient;

nit(
   '<your_api_key>',
   '<base_url>', // optional, default https://fscdn.eppo.cloud/api
   $assignmentLogger, // optional, must be an instance of Eppo\Logger\LoggerInterface
   $cache // optional, must be an instance of PSR-16 SimpleCache\CacheInterface. If not passed, FileSystem cache will be used
   $httpClient // optional, must be an instance of PSR-18 ClientInterface. If not passed, Discovery will be used to find a suitable implementation
   $requestFactory // optional, must be an instance of PSR-17 Factory. If not passed, Discovery will be used to find a suitable implementation
);

$subjectAttributes = [ 'tier' => 2 ];
$assignment = $eppoClient->getStringAssignment('experimentalBackground', 'user123', $subjectAttributes, 'defaultValue');

if ($assignment !== 'defaultValue') {
    // do something
}

$subjectContext = [
    'age' => 30, // Gets interpreted as a Numeric Attribute
    'country' => 'uk', // Categorical Attribute
    'pricingTier' => '1'  // NOTE: Deliberately setting to string causes this to be treated as a Categorical Attribute
];

$actionContexts = [
    'nike' => [
        'brandLoyalty' => 0.4,
        'from' => 'usa'
    ],
    'adidas' => [
        'brandLoyalty' => 2,
        'from' => 'germany'
    ]
];

$result = $client->getBanditAction(
    'flagKey',
    'subjectKey',
    $subjectContext,
    $actionContexts,
    'defaultValue'
);

if ($result->action != null) {
    // Follow the Bandit action
    doAction($result->action);
} else {
    doSomething($result->variation);
}

getBooleanAssignment(...)
getNumericAssignment(...)
getIntegerAssignment(...)
getStringAssignment(...)
getJSONAssignment(...)

function getBooleanAssignment(
    string $flagKey,
    string $subjectKey,
    array $subjectAttributes,
    bool $defaultValue
): bool



use Eppo\Logger\LoggerInterface;


use Eppo\Logger\AssignmentEvent;
use Eppo\Logger\LoggerInterface;

class SegmentLogger implements LoggerInterface
{
    public function logAssignment(AssignmentEvent $assignmentEvent): void
    {
        Segment::track([
            'event' => 'Flag Assignment for ' . $assignmentEvent->featureFlag,
            'userId' => $assignmentEvent->subject,
            'properties' => $assignmentEvent->toArray()
        ]);
    }
}



use Eppo\Logger\AssignmentEvent;
use Eppo\Logger\BanditActionEvent;
use Eppo\Logger\IBanditLogger;

class SegmentLogger implements IBanditLogger
{
    public function logAssignment(AssignmentEvent $assignmentEvent): void
    {
        Segment::track([
            'event' => 'Flag Assignment for ' . $assignmentEvent->featureFlag,
            'userId' => $assignmentEvent->subject,
            'properties' => $assignmentEvent->toArray()
        ]);
    }

    public function logBanditAction(BanditActionEvent $banditActionEvent): void
    {
        Segment::track([
            'event' => 'Bandit Action Selected',
            'userId' => $banditActionEvent->subjectKey,
            'properties' => $banditActionEvent->toArray()
        ]);
    }
}

$eppoClient = EppoClient::init(
   '<your_api_key>',
   '<base_url>', // optional, default https://fscdn.eppo.cloud/api
   $assignmentLogger, // optional, must be an instance of Eppo\LoggerInterface
   $cache // optional, must be an instance of PSR-16 SimpleInterface. If not passed, FileSystem cache will be used
   $httpClient // optional, must be an instance of PSR-18 ClientInterface. If not passed, Discovery will be used to find a suitable implementation
   $requestFactory // optional, must be an instance of PSR-17 Factory. If not passed, Discovery will be used to find a suitable implementation
);

$eppoClient->startPolling();
shell
composer 
shell
php eppo-poller.php