PHP code example of kameleoon / openfeature-php

1. Go to this page and download the library: Download kameleoon/openfeature-php 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/ */

    

kameleoon / openfeature-php example snippets


use OpenFeature\implementation\flags\Attributes;
use OpenFeature\implementation\flags\EvaluationContext;
use OpenFeature\OpenFeatureAPI;
use Kameleoon\KameleoonProvider;
use Kameleoon\KameleoonClientConfig;

$clientConfig = new KameleoonClientConfig(
    "clientId",
    "clientSecret",
);

$provider = new KameleoonProvider('siteCode', $clientConfig);
$api = OpenFeatureAPI::getInstance();
$api->setProvider($provider);
$client = $api->getClient();

$dataDictionary = [
    'variableKey' => 'stringKey'
];
$evalContext = new EvaluationContext("visitorCode", new Attributes($dataDictionary));

$evaluationDetails = $client->getStringDetails("featureKey", 5, $evalContext);

$numberOfRecommendedProducts = $evaluationDetails->getValue();
print_r("Number of recommended products: " . $numberOfRecommendedProducts);

$clientConfig = new KameleoonClientConfig(
    clientId: "clientId",
    clientSecret: "clientSecret",
    kameleoonWorkDir: "/tmp/kameleoon/php-client/", // kameleoonWorkDir: optional / ("/tmp/kameleoon/php-client/" by default)
    refreshIntervalMinute: 60, // refreshIntervalMinute: in minutes, optional (60 minutes by default)
    defaultTimeoutMillisecond: 10_000, // defaultTimeoutMillisecond: in milliseconds, optional (10_000 ms by default)
    debugMode: false, // debugMode: optional (false by default)
    cookieOptions: $cookieOptions, // cookieOptions: optional
    environment: "development" // environment: optional ("production" by default)
);

$provider = new KameleoonProvider('siteCode', $clientConfig);

$values = [
  'variableKey' => 'stringKey'
];

$evalContext = new EvaluationContext("userId", new Attributes($values));

$customeDataDictionary = [
    DataType::CUSTOM_DATA => [
        CustomDataType::INDEX => 1,
        CustomDataType::VALUES => '10'
    ]
];

$evalContext = new EvaluationContext("userId", new Attributes($customeDataDictionary));

$conversionDictionary = [
    DataType::CONVERSION => [
        ConversionType::GOAL_ID => 1,
        ConversionType::REVENUE => 200
    ]
];

$evalContext = new EvaluationContext("userId", new Attributes($conversionDictionary));

$dataDictionary = [
    DataType::CONVERSION => [
        ConversionType::GOAL_ID => 1,
        ConversionType::REVENUE => 200
    ],
    DataType::CUSTOM_DATA => [
        [
            CustomDataType::INDEX => 1,
            CustomDataType::VALUES => ['10', '30']
        ],
        [
            CustomDataType::INDEX => 2,
            CustomDataType::VALUES => '20'
        ]
    ]
];

$evalContext = new EvaluationContext("userId", $dataDictionary);