Sample code for PHP
Getting user details, finding indicators, creating and deleting scores
<?php
/*
* This is an example how to use the KPI Dashboard API.
* Language: PHP
*
* This sample code shows how to lookup an existing indicator
* Then it creates a score for this indicator.
* At the end it removes the created score again.
* ------------------------------------------------------------------
* You will need to adjust the MY_API_TOKEN value:
*
* MY_API_TOKEN - Your API token, as displayed on your
* "Personal Settings" page in KPI Dashboard.
* ------------------------------------------------------------------
* Feel free to use this code and modify to fit your needs.
* For more information visit http://dashboard.kpilibrary.com/api
* ------------------------------------------------------------------
*/
define('API_URI', 'https://dashboard.kpilibrary.com/api/v2');
// ------------------------------------------------------------------
// Replace the info below with your own:
define('MY_API_TOKEN', 'find-your-key-in-your-personal-settings-page');
// ------------------------------------------------------------------
print("\nGetting my user info ...");
showMyOwnUserDetails();
print("\n\nSelecting first indicator ...");
list($indicatorId, $indicatorName) = getIndicator();
print("\nUsing indicator \"$indicatorName\" with id $indicatorId");
print("\n\nCreating score of 33.39 for 2010-09-30 ...");
$scoreId = PostScore($indicatorId, "33.39", "2010-09-30");
print("\nCreated score $scoreId");
print("\nScores:");
ShowScores($indicatorId);
print("\n\nDeleting score $scoreId ...");
DeleteScore($scoreId);
print("\nDeleted score $scoreId");
print("\nScores:");
ShowScores($indicatorId);
print("\n\nDone.\n\n");
//-----------------------------------------------------------------------//
function showMyOwnUserDetails() {
$response = show("/me.xml");
$xml = new SimpleXMLElement($response);
$result = $xml->xpath('/users/user');
while(list( , $user) = each($result)) {
print("\n$user->name has access to " . $user->{"tenant-name"} . " as $user->role");
}
}
//-----------------------------------------------------------------------//
function ShowScores($indicatorId) {
$response = show("/indicators/$indicatorId/scores.xml");
$xml = new SimpleXMLElement($response);
$result = $xml->xpath('/scores/score');
while(list( , $score) = each($result)) {
print("\nScore id:$score->id value:$score->value period:$score->period");
}
}
//-----------------------------------------------------------------------//
function getIndicator() {
$response = show("/indicators.xml");
$xml = new SimpleXMLElement($response);
$result = $xml->xpath('/indicators/indicator');
if (count($result) == 0) {
throw new Exception("Error: No indicators are defined in KPI Dashboard. " +
"Try loading the demo data.");
}
while(list( , $indicator) = each($result)) {
// for demo purposes just return the first indicator //
return array($indicator->id, $indicator->name);
}
}
//-----------------------------------------------------------------------//
function postScore($indicatorId, $value, $date) {
$uri = "/indicators/$indicatorId/scores.xml";
$data = "<score><value>$value</value><date>$date</date></score>";
$response = create($uri, $data);
$xml = new SimpleXMLElement($response);
return($xml[0]->id);
}
//-----------------------------------------------------------------------//
function DeleteScore($scoreId) {
delete("/scores/$scoreId.xml");
}
//-----------------------------------------------------------------------//
function show($pUrl) {
return send($pUrl, 'GET');
}
//-----------------------------------------------------------------------//
function create($pUrl, $xmlData) {
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . $xmlData;
return send($pUrl, 'POST', $xml);
}
//-----------------------------------------------------------------------//
function put($pUrl, $xmlData) {
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . $xmlData;
return send($pUrl, 'PUT', $xml);
}
//-----------------------------------------------------------------------//
function delete($pUrl) {
return send($pUrl, 'DELETE');
}
//-----------------------------------------------------------------------//
function send($pUrl, $method, $data = "", $page = "1") {
$url = API_URI . $pUrl;
$headers = array(
'Content-Type: application/xml; charset=utf-8',
);
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($handle, CURLOPT_USERPWD, MY_API_TOKEN . ':x');
switch($method)
{
case 'GET':
$url .= "?page=" . $page . "&per_page=100";
curl_setopt($handle, CURLOPT_URL, $url);
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
if (!($code == 200 | $code == 201)) {
$xml = new SimpleXMLElement($response);
$result = $xml->xpath('/errors/error');
$msg = '';
while(list( , $error) = each($result)) {
$msg .= "$error\n";
}
throw new Exception("HTTP $code: $msg\n", $code);
}
return $response;
}
//-----------------------------------------------------------------------//
?>