PHP code example of vwo / vwo-fme-php-sdk

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

    

vwo / vwo-fme-php-sdk example snippets


$vwoClient = VWO::init([
  'sdkKey' => 'vwo_sdk_key',
  'accountId' => 'vwo_account_id',
]);

// set user context
$userContext = [ 'id' => 'unique_user_id'];

// returns a flag object
$getFlag = $vwoClient->getFlag('feature_key', $userContext);

// check if flag is enabled
$isFlagEnabled = $getFlag['isEnabled'];

// get variable
$variableValue = $getFlag->getVariable('variable_key', 'default-value');

// track event
$trackRes = $vwoClient->trackEvent('event_name', $userContext);

// set Attribute
$attributes = [
  'attribute_key' => 'attribute_value'
];
$setAttribute = $vwoClient->setAttribute($attributes, $userContext);

$userContext = [
  'id' => 'unique_user_id',
  'customVariables' => ['age' => 25, 'location' => 'US'],
  'userAgent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
  'ipAddress' => '1.1.1.1',
];

$featureFlag = $vwoClient->getFlag('feature_key', $userContext);
$isEnabled = $featureFlag->isEnabled();

if ($isEnabled) {
  echo 'Feature is enabled!';

  // Get and use feature variable with type safety
  $variableValue = $featureFlag->getVariable('feature_variable', 'default_value');
  echo 'Variable value:', $variableValue;
} else {
  echo 'Feature is not enabled!';
}

$vwoClient->trackEvent('event_name', $userContext, $eventProperties);

$vwoClient->setAttribute('attribute_name', 'attribute_value', $userContext);

$attributes = [
  'attribute_name' => 'attribute_value'
];
$vwoClient->setAttribute($attributes, $userContext);

$vwoClient = VWO::init([
  'sdkKey' => '32-alpha-numeric-sdk-key',
  'accountId' => '123456',
  'pollInterval' => 60000,
]);

$vwoClient = VWO::init([
  'sdkKey' => '32-alpha-numeric-sdk-key',
  'accountId' => '123456',
  'gatewayService' => [
    'url' => 'https://custom.gateway.com',
  ],
]);

class StorageConnector {
   private $map = [];


   public function get($featureKey, $userId) {
    $key = $featureKey . '_' . $userId;
    return isset($this->map[$key]) ? $this->map[$key] : null;
   }


   public function set($data) {
    $key = $data['featureKey'] . '_' . $data['user'];
    // Implement your storage logic here to store the data in your preferred database system using $key
   }
}

// Initialize the StorageConnector
$storageConnector = new StorageConnector();

$vwoClient = VWO::init([
  'sdkKey' => '32-alpha-numeric-sdk-key',
  'accountId' => '123456',
  'storage' => $storageConnector,
]);

$vwoClient1 = VWO::init([
  'sdkKey' => '32-alpha-numeric-sdk-key',
  'accountId' => '123456',
  'logger' => [
    'level' => 'DEBUG',
  ],
]);

$vwoClient2 = VWO::init([
  'sdkKey' => '32-alpha-numeric-sdk-key',
  'accountId' => '123456',
  'logger' => [
    'level' => 'DEBUG',
    'prefix' => 'CUSTOM LOG PREFIX',
  ],
]);

$vwoClient3 = VWO::init([
  'sdkKey' => '32-alpha-numeric-sdk-key',
  'logger' => [
    'transport' => [
        'level' => 'DEBUG',
        'logHandler' => function($msg, $level){
          echo "$msg $level";
        }
    ],
  ]
]);

$vwoClient3 = VWO::init([
  'sdkKey' => '32-alpha-numeric-sdk-key',
  'logger' => [
    'transports' => [
        [
            'level' => 'DEBUG',
            'logHandler' => function($msg, $level){
              echo "$msg $level";
            }
        ],
        [
            'level' => 'INFO',
            'logHandler' => function($msg, $level){
              echo "$msg $level";
            }
        ]
    ]
  ]
]);