This repo contains the OpenFeature PHP flag provider for Confidence.
Refer to the Confidence documentation for more information. Before starting to use the provider, it can be helpful to read through the general OpenFeature docs and get familiar with the concepts.
This library supports the same platforms as the OpenFeature PHP SDK. Requires PHP >= 8.2.
Note: This provider uses the online resolver approach. Flag evaluations are resolved by making API calls to the Confidence backend for each evaluation request. This is different from the local resolver approach used in other Confidence providers (Java, JavaScript, Go) which use WebAssembly (WASM) for local flag evaluation.
Install the package via Composer:
composer require spotify/confidence-openfeature-providerBelow is an example for how to create an OpenFeature client using the Confidence flag provider, and then resolve a flag with a boolean attribute. The provider is configured with a client secret and a base URL, which will determine where it will send the resolving requests.
The flag will be applied immediately, meaning that Confidence will count the targeted user as having received the treatment.
You can retrieve attributes on the flag variant using property dot notation, meaning test-flag.boolean-key will retrieve the attribute boolean-key on the flag test-flag.
You can also use only the flag name test-flag and retrieve all values as an array with resolveObjectValue().
<?php
require_once 'vendor/autoload.php';
use Confidence\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";You can point the provider at a local or custom resolver by passing any base URL:
$apiClient = new ApiClient(
clientSecret: getenv('CONFIDENCE_CLIENT_SECRET'),
baseUrl: 'http://localhost:8080/v1',
);The evaluation context contains information about the user/session being evaluated for targeting and A/B testing.
$context = new EvaluationContext(attributes: new Attributes([
'user_id' => 'user-123',
'country' => 'US',
'plan' => 'premium',
'age' => 25,
]));Important: This provider uses the online resolver approach - each flag evaluation makes a network call to Confidence. Proper error handling is critical!
The provider maps errors to standard OpenFeature error codes:
FLAG_NOT_FOUND- The requested flag does not exist or is not activeTYPE_MISMATCH- The flag value type does not match the requested type, or the key path is invalidGENERAL- Network or API errors
$details = $client->getBooleanDetails('test-flag.enabled', false, $context);
if ($details->getError() !== null) {
error_log(sprintf(
'Flag error: %s (%s)',
$details->getError()->getResolutionErrorMessage(),
$details->getReason(),
));
}