Mapping Active Campaigns and Variation Group IDs to their Names

The following code is written in PHP and assumes that the client knows only the Site ID.

  1. The client goes to the discovery page and retrieves the Campaigns collection resource through the “campaigns” link relation and “active-running” name.
  2. The client retrieves all the Campaigns, loops through each Campaign to retrieve all the Variation Groups for each Campaign through the “variationgroups” link relation, and prints ID and names to the screen.
<?php

//function for making cURL requests
function curl($url,$method,$data){

//cURL call
$ch = curl_init();

if($method){
curl_setopt ($ch, CURLOPT_POST, true);
if($data){
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
}
}

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-API-TOKEN: YOUR-API-TOKEN"));

$output = curl_exec($ch);

if($output !== false || curl_error($ch)) {

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($status,$output);

curl_close($ch);
}

}

//list of known variables – Site ID
$site = "5";

//Location of the discovery page
$discovery = "http://admin1.sitespect.com/api"";


//find the variationgroup and campaign resource URI from the discovery page
$request = curl($discovery);

if ($request[0] == 200){
$json = json_decode($request[1],true);

//get active-running campaing collection URI
$campaigns_all = $json['_links']['campaigns'];

//find the campaing collection URI for active-running campaigns
foreach($campaigns_all as $campaigns_URI) {
if($campaigns_URI['name']=='active-running'){
$campaings_active_URI = $campaigns_URI['href'];
}
}

//apply site variable to the URI template
$campaings_active_call = str_replace('{site-id}', $site, $campaings_active_URI);

//Retrieve all the campaigns
$req = curl($campaings_active_call);

if ($req[0] == 200){
$json = json_decode($req[1],true);

//loop through each campaign in the embedded object
foreach($json['_embedded']['campaigns'] as $campaign) {

$campaign_ID = $campaign['ID'];
$campaign_name = $campaign['Name'];

echo 'Campaign ID: '.$campaign_ID.'<br>';
echo 'Campaign Name: '.$campaign_name.'<br><br>';

$variationgroups_call = $campaign['_links']['variationgroups']['href'];

//process the variatongroup call for each campaing to get Variation Group information
$var = curl($variationgroups_call);
if ($var[0] == 200){
$varjson = json_decode($var[1],true);

//loop through each variation group to get ID and name mapping
foreach($varjson['_embedded']['variationgroups'] as $variationgroup) {

$variationgroup_ID = $variationgroup['ID'];
$variationgroup_name = $variationgroup['Name'];

echo 'Variation Group ID: '.$variationgroup_ID.'<br>';
echo 'Variation Group Name: '.$variationgroup_name.'<br>';
}

//formatiing
echo "<br>";
}
}

}

}
?>