Sample code to consume the Device API using PHP.

$API_KEY = 'YOUR API KEY';
$SECRET = 'YOUR SECRET KEY';

//GET ALL DEVICES
GetDevice(null);
//GET DEVICE BY ID
GetDevice(1);
function GetDevice($deviceID) {
global $API_KEY;
global $SECRET;
$apiHost = "https://www.kbremote.net"; //API HOST
$apiPath = "api"; //PATH TO API
$apiFunction = "device"; //API FUNCTION NAME
$url = $apiHost . "/" . $apiPath . "/" . $apiFunction;
if (!is_null($deviceID))
$url = $url . "/" . $deviceID;
//GET UTC TIME
$timeStamp = gmdate("Y-m-d H:i:s");
//CREATE MESSAGE IN STANDARD FORMAT TO HASH
$message = "GET" . $timeStamp . "/" . $apiPath . "/" . $apiFunction;
if (!is_null($deviceID))
$message = $message . "/" . $deviceID;
$hash_mac = hash_hmac('sha256', $message, $SECRET, true);
$messageHash = base64_encode($hash_mac);
$headers = array(
'Authentication: ' . $API_KEY . ":" . $messageHash,
'Timestamp: ' . $timeStamp
);
// INITIALIZE CURL HANDLE
$ch = curl_init();
// SET URL
curl_setopt( $ch, CURLOPT_URL, $url );
// SET CUSTOM HEADERS
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
//GET RESPONSE BACK AS STRING
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
// SEND REQUEST
$result = curl_exec( $ch );

//GET HTTP RESPONSE CODE
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// DISPLAY ERRORS IF YOU WISH
if (curl_errno($ch)) {
echo curl_error( $ch );
}
//CLOSE CURL HANDLE
curl_close($ch);

//CHECK IF REQUEST WAS SUCCESSFUL
if ($responseCode == "200") {
$json = json_decode($result);
if (is_null($deviceID)) {
foreach ($json as $key => $jsons) { // LOOP THROUGH DEVICES AND VALUES
foreach($jsons as $key => $value) {
echo $key . ': ';
echo $value;
echo '<br>';
}
echo '<br><br>';
}
}
else
{
foreach ($json as $key => $value) { //JUST LOOP THROUGH VALUES IF ONLY EXPECTING SINGLE DEVICE
echo $key . ': ';
echo $value;
echo '<br>';
}
}
}
else {
echo "Error HTTP Response Code " . $responseCode;
}
}
Was this article helpful?
Thank you!