Skip to main content

PHP Server SDK Usage

Packagist GitHub

User Object

The full user data must be passed into every method. The only required field is user_id. The rest are optional and are used by the system for user segmentation into variables and features.

See the User model in the PHP user model doc for all accepted fields including custom fields.

use DevCycle\Model\DevCycleUser;

$user_data = new DevCycleUser(array("user_id"=>"my-user"));

Get and use Variable by key

To get values from your Variables, variableValue() is used to fetch variable values using the user data, variable key, coupled with a default value for the variable. The default variable will be used in cases where the user is not segmented into a feature using that variable, or the project configuration is unavailable to be fetched from DevCycle's CDN.

try {
$result = $devcycleClient->variableValue($user_data, "my-key", "default");
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DevCycleClient->variableValue: ', $e->getMessage(), PHP_EOL;
}

The default value can be of type string, boolean, number, or object.

If you would like to get the full Variable object defined by getVariableByKey you can use variable() instead of variableValue().

Get all Variables

try {
$result = $devcycleClient->allVariables($user_data);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DevCycleClient->allVariables: ', $e->getMessage(), PHP_EOL;
}

See getVariables on the Bucketing API for the variable response format.

caution

This method is intended to be used for debugging and analytics purposes, not as a method for retrieving the value of Variables to change code behaviour. For that purpose, we strongly recommend using the individual variable access method described in Get and use Variable by key Using this method instead will result in no evaluation events being tracked for individual variables, and will not allow the use of other DevCycle features such as Code Usage detection

Getting all Features

try {
$result = $devcycleClient->allFeatures($user_data);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DevCycleClient->allFeatures: ', $e->getMessage(), PHP_EOL;
}

See getFeatures on the Bucketing API for the feature response format.

Track Event

use DevCycle\Model\DevCycleEvent;

$event_data = new DevCycleEvent(array("type"=>"my-event"));

try {
$result = $devcycleClient->track($user_data, $event_data);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling DevCycleClient->track: ', $e->getMessage(), PHP_EOL;
}

EdgeDB

EdgeDB allows you to save user data to our EdgeDB storage so that you don't have to pass in all the user data every time you identify a user. Read more about EdgeDB.

To get started, contact us at [email protected] to enable EdgeDB for your project.

Once you have EdgeDB enabled in your project, pass in the enableEdgeDB option to turn on EdgeDB mode for the SDK:


use DevCycle\Api\DevCycleClient;
use DevCycle\Model\DevCycleOptions;
use DevCycle\Model\DevCycleUser;

$options = new DevCycleOptions(true);
$devCycleClient = new DevCycleClient(
sdkKey: getenv("DEVCYCLE_SERVER_SDK_KEY"),
dvcOptions: $options);

Async Methods

Each method in the Usage section has a corresponding asynchronous method:

$result = $devcycleClient->allVariables($user_data);
$devcycleClient->allVariablesAsync($user_data)->then(function($result) {
print_r($result);
});

Models

User

User data is provided to most SDK requests to identify the user / context of the feature evaluation

NameTypeDescriptionNotes
user_idStringUnique id to identify the user
emailStringUser's email used to identify the user on the dashboard / target audiences[optional]
nameStringUser's name used to identify the user on the dashboard / target audiences[optional]
languageStringUser's language in ISO 639-1 format[optional]
countryStringUser's country in ISO 3166 alpha-2 format[optional]
app_versionStringApp Version of the running application[optional]
app_buildStringApp Build number of the running application[optional]
custom_dataObjectUser's custom data to target the user with, data will be logged to DevCycle for use in dashboard.[optional]
private_custom_dataObjectUser's custom data to target the user with, data will not be logged to DevCycle only used for feature bucketing.[optional]
created_dateFloatDate the user was created, Unix epoch timestamp format[optional]
last_seen_dateFloatDate the user was created, Unix epoch timestamp format[optional]
platformStringPlatform the Client SDK is running on[optional]
platform_versionStringVersion of the platform the Client SDK is running on[optional]
device_modelStringUser's device model[optional]
sdk_typeStringDevCycle SDK type[optional]
sdk_versionStringDevCycle SDK Version[optional]

Event

Event data is provided to track calls to log events to DevCycle

NameTypeDescriptionNotes
typeStringCustom event type
targetStringCustom event target / subject of event. Contextual to event type[optional]
dateFloatUnix epoch time the event occurred according to client[optional]
valueFloatValue for numerical events. Contextual to event type[optional]
meta_dataObjectExtra JSON metadata for event. Contextual to event type[optional]

Variable

Variable objects are returned by the SDK when calling variable or allVariables.

NameTypeDescriptionNotes
_idStringunique database id
keyStringUnique key by Project, can be used in the SDK / API to reference by 'key' rather than _id.
typeStringVariable type
valueObjectVariable value can be a string, number, boolean, or JSON

Feature

Feature objects are returned by the SDK when calling allFeatures

NameTypeDescriptionNotes
_idStringunique database id
keyStringUnique key by Project, can be used in the SDK / API to reference by 'key' rather than _id.
typeStringFeature type
_variationStringBucketed feature variation
eval_reasonStringEvaluation reasoning[optional]

Tests

To run the tests, use:

composer install
vendor/bin/phpunit