Skip to content

Instantly share code, notes, and snippets.

@tore-statsig
Last active November 30, 2021 01:13
Show Gist options
  • Save tore-statsig/c27fb9de65c7a1d1591d161570ae807a to your computer and use it in GitHub Desktop.
Save tore-statsig/c27fb9de65c7a1d1591d161570ae807a to your computer and use it in GitHub Desktop.
statsig.php - a simple php wrapper around the http api
<?php
// Usage
// $user = (object)[];
// $user->userID = 3;
// checkGate($user, "gate_name");
// getConfig($user, "config_name"));
// getExperiment($user, "product_logo_icon_shapes");
// $meta = (object)[];
// $meta->test = "testing123";
// logEvent($user, "test", 123, $meta);
$GLOBALS['statsig_api_key'] = "<YOUR_API_KEY_HERE>";
function _statsigApi($key, $endpoint, $input) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.statsig.com/v1/{$endpoint}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $input,
CURLOPT_HTTPHEADER => array(
"statsig-api-key: {$key}",
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
$json_response = json_decode($response, true);
return $json_response;
}
function checkGate($user, $gate) {
$req_body = (object)[];
$req_body->user = $user;
$req_body->gateName = $gate;
$json_response = _statsigApi($GLOBALS['statsig_api_key'], 'check_gate', json_encode($req_body));
return $json_response["value"];
}
function getConfig($user, $config) {
$req_body = (object)[];
$req_body->user = $user;
$req_body->configName = $config;
$json_response = _statsigApi($GLOBALS['statsig_api_key'], 'get_config', json_encode($req_body));
return $json_response["value"];
}
function getExperiment($user, $config) {
return getConfig($user, $config);
}
function logEvent($user, $event_name, $value, $metadata) {
$event = (object)[];
$event->eventName = $event_name;
$event->value = $value;
$event->metadata = $metadata;
$req_body = (object)[];
$req_body->user = $user;
$req_body->events = [$event];
_statsigApi($GLOBALS['statsig_api_key'], 'log_event', json_encode($req_body));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment