PHP code example of gathern / growthbook-openfeature-provider

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

    

gathern / growthbook-openfeature-provider example snippets



    use Cache\Adapter\Apcu\ApcuCachePool;
    use Growthbook\Growthbook;
    use OpenFeature\OpenFeatureAPI;

    $growthbook Growthbook::create();
    //By default, there is no caching enabled.
    // You can enable it by passing any PSR16-compatible
    // instance into the withCache method.
    
    // Any psr-16 library will work
    $cache = new ApcuCachePool();

    $growthbook ->withCache($cache);

    $api = OpenFeatureAPI::getInstance();
    $api->setProvider(new GrowthbookOpenfeatureProvider(
            growthbook: Growthbook,
            clientKey: '<Growthbook_CLIENT_KEY>',
            apiHost: '<Growthbook_API_HOST>',
        ));

    $client = $api->getClient(
        GrowthbookOpenfeatureProvider::class,
        'v1.17',
         );

    $client->getStringValue
    (
        flagKey: 'enable-success-button',
        defaultValue: 'wrong',
    );



    use OpenFeature\implementation\flags\Attributes;
    use OpenFeature\implementation\flags\MutableEvaluationContext;

        $user_data = [
            'name' => 'john Doe',
            'age' => '20',
            'gender' => 'male',
            'nationality' => 'martian'
            ];

       echo $client->getBooleanValue(
                flagKey: 'test-boolean',
                defaultValue: false,
                context: new MutableEvaluationContext(
                    targetingKey: "targeting-key-value",
                    attributes: new Attributes(attributesMap: $user_data)
                )
            );




$openfeatureAPI= invade($client)->api;
$provider=invade($openfeatureAPI)->getProvider();
$growthbook=invade($provider)->growthbook;
print_r($growthbook->getFeatures());
print_r($growthbook->getAttributes());
print_r( $growthbook->getViewedExperiments());