PHP

Getting started

1. Install the package with Composer

composer require configcat/configcat-client

2. Create the ConfigCat client with your SDK Key

$client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#");

3. Get your setting value:

$isMyAwesomeFeatureEnabled = $client->getValue("isMyAwesomeFeatureEnabled", false);
if(is_bool($isMyAwesomeFeatureEnabled) && $isMyAwesomeFeatureEnabled) {
doTheNewThing();
} else {
doTheOldThing();
}

Creating the ConfigCat Client

ConfigCat Client is responsible for:

  • managing the communication between your application and ConfigCat servers.
  • caching your setting values and feature flags.
  • serving values quickly in a failsafe way.

Constructor parameters:

NameTypeDescription
sdkKeystringREQUIRED. SDK Key to access your feature flags and configurations. Get it from ConfigCat Dashboard.
optionsarrayOptional. Additional configuration options, see below for the detailed list.

Available configuration options:

NameTypeDescription
data-governanceintOptional, defaults to DataGovernance::GLOBAL_. Describes the location of your feature flag and setting data within the ConfigCat CDN. This parameter needs to be in sync with your Data Governance preferences. More about Data Governance. Available options: GLOBAL_, EU_ONLY.
loggerPsr\Log\LoggerInterfaceConfigures a logger for errors and warnings produced by the SDK, defaults to Psr\Log\NullLogger.
cacheConfigCat\Cache\ConfigCacheSets a ConfigCat\Cache\ConfigCache implementation for caching the actual configurations. You can check the currently available implementations here.
cache-refresh-intervalintSets the refresh interval of the cache in seconds, after the initial cached value is set this value will be used to determine how much time must pass before initiating a new configuration fetch request. Defaults to 60.
request-optionsarraySets the options for the request initiated by theGuzzle HTTP client.

Example:

$client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#", [
'cache' => new \ConfigCat\Cache\LaravelCache(Cache::store()),
'cache-refresh-interval' => 5
]);

Anatomy of getValue()

ParametersDescription
keyREQUIRED. Setting-specific key. Set on ConfigCat Dashboard for each setting.
defaultValueREQUIRED. This value will be returned in case of an error.
userOptional, User Object. Essential when using Targeting. Read more about Targeting.
$value = $client->getValue(
"keyOfMySetting", # Setting Key
false, # Default value
new \ConfigCat\User('435170f4-8a8b-4b67-a723-505ac7cdea92') # Optional User Object
);

User Object

The User Object is essential if you'd like to use ConfigCat's Targeting feature.

$user = new \ConfigCat\User("435170f4-8a8b-4b67-a723-505ac7cdea92");
$user = new \ConfigCat\User("john@example.com");
ParametersDescription
identifierREQUIRED. Unique identifier of a user in your application. Can be any value, even an email address.
emailOptional parameter for easier targeting rule definitions.
countryOptional parameter for easier targeting rule definitions.
customOptional array for custom attributes of a user for advanced targeting rule definitions. e.g. User role, Subscription type.
$user = new \ConfigCat\User(
'435170f4-8a8b-4b67-a723-505ac7cdea92',
'john@example',
'United Kingdom',
[
'SubscriptionType' => 'Pro',
'UserRole' => 'Admin'
]);

getAllKeys()

You can query the keys from your configuration in the SDK with the getAllKeys() method.

$client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#");
$keys = $client->getAllKeys();

Cache

You can use the following caching options:

  • Laravel:

    $client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#", [
    'cache' => new \ConfigCat\Cache\LaravelCache(\Illuminate\Support\Facades\Cache::store()),
    ]);
  • PSR-6 cache (e.g. the redis adapter for PSR-6):

    $client = new \RedisArray(['127.0.0.1:6379', '127.0.0.2:6379']);
    $pool = new RedisCachePool($client);
    $client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#", [
    'cache' => new \ConfigCat\Cache\Psr6Cache($pool),
    ]);

    or with the file system adapter:

    $filesystemAdapter = new League\Flysystem\Adapter\Local(__DIR__.'/');
    $filesystem = new League\Flysystem\Filesystem($filesystemAdapter);
    $pool = new Cache\Adapter\Filesystem\FilesystemCachePool($filesystem);
    $client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#", [
    'cache' => new \ConfigCat\Cache\Psr6Cache($pool),
    ]);
  • PSR-16 cache (e.g. the redis adapter for PSR-6 and the PSR-6 to PSR-16 cache bridge):

    $client = new \RedisArray(['127.0.0.1:6379', '127.0.0.2:6379']);
    $pool = new RedisCachePool($client);
    $simpleCache = new SimpleCacheBridge($pool);
    $client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#", [
    'cache' => new \ConfigCat\Cache\Psr16Cache($simpleCache),
    ]);

    or with the file system adapter:

    $filesystemAdapter = new League\Flysystem\Adapter\Local(__DIR__.'/');
    $filesystem = new League\Flysystem\Filesystem($filesystemAdapter);
    $pool = new Cache\Adapter\Filesystem\FilesystemCachePool($filesystem);
    $simpleCache = new SimpleCacheBridge($pool);
    $client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#", [
    'cache' => new \ConfigCat\Cache\Psr16Cache($simpleCache),
    ]);
  • Custom cache implementation

    class CustomCache extends ConfigCache
    {
    protected function get($key)
    {
    // load from cache
    }
    protected function set($key, $value)
    {
    // save into cache
    }
    }

Logging

The SDK uses the PSR-3 LoggerInterface for logging, so you can use any implementor package like Monolog.

$client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#", [
'logger' => new \Monolog\Logger("name"),
]);

HTTP Client (Proxy)

The SDK uses Guzzle for the underlying HTTP calls and its request options are available to customize through the SDK options like:

$client = new \ConfigCat\ConfigCatClient("#YOUR-SDK-KEY#", [
'request-options' => [
'proxy' => [
'http' => 'tcp://localhost:8125',
'https' => 'tcp://localhost:9124',
]
],
]);

Sample Applications

Look under the hood