PHP code example of spotify / confidence-openfeature-provider

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

    

spotify / confidence-openfeature-provider example snippets




onfidence\OpenFeature\ApiClient;
use Confidence\OpenFeature\ConfidenceProvider;
use Confidence\OpenFeature\Region;
use OpenFeature\implementation\flags\Attributes;
use OpenFeature\implementation\flags\EvaluationContext;
use OpenFeature\OpenFeatureAPI;

// Configure OpenFeature with Confidence provider
$api = OpenFeatureAPI::getInstance();

$apiClient = new ApiClient(
    clientSecret: getenv('CONFIDENCE_CLIENT_SECRET'),
    baseUrl: Region::EU->value,
);
$api->setProvider(new ConfidenceProvider($apiClient));

// Create a client
$client = $api->getClient('my-app');

$context = new EvaluationContext(attributes: new Attributes([
    'user_id' => 'user-123',
    'country' => 'SE',
    'plan' => 'premium',
]));

$flagValue = $client->getBooleanValue('test-flag.boolean-key', false, $context);

echo "Feature enabled: " . ($flagValue ? 'true' : 'false') . "\n";

$apiClient = new ApiClient(
    clientSecret: getenv('CONFIDENCE_CLIENT_SECRET'),
    baseUrl: 'http://localhost:8080/v1',
);

$context = new EvaluationContext(attributes: new Attributes([
    'user_id' => 'user-123',
    'country' => 'US',
    'plan' => 'premium',
    'age' => 25,
]));

$details = $client->getBooleanDetails('test-flag.enabled', false, $context);

if ($details->getError() !== null) {
    error_log(sprintf(
        'Flag error: %s (%s)',
        $details->getError()->getResolutionErrorMessage(),
        $details->getReason(),
    ));
}