Forum Discussion

Jeff_Woeber's avatar
8 years ago

Using LogicMonitor's API through PHP

This is a great site that has a pre-configured PHP environment for testing.  
 
A handy EPOC time converter
 
Example PHP API script that will create a SDT for device ID 92.  
<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
$accessID = 'u6MyF2T3Q9csqufyG79Z';
$accessKEY = '******************************';
$resourcePath = "/sdt/sdts";
$epoch = round(microtime(true) * 1000);
$httpVerb = "POST";
$data = array("type" => "DeviceSDT", "sdtType" => 1, "startDateTime" => 1501794715, "endDateTime" => 1502488624000, "deviceDisplayName" => "CentOSTEMP", "deviceId" => 92);
$data_string = json_encode($data, true);
$requestVars = $httpVerb. $epoch. $data_string. $resourcePath;
// Generate Signature
$sig = base64_encode(hash_hmac('sha256', $requestVars, $accessKEY));
// Setup headers
$auth = 'LMv1 '.$accessID.
':'.$sig.
':'.$epoch;
$headers = array('Content-Type: application/json', 'Authorization: '.$auth);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
</body>
</html>
The return looks like:

    "endHour" : 0,                                                                                                                                                                                                                                                  
    "endMinute" : 0,                                                                                                                                                                                                                                                
    "duration" : 25016447,                                                                                                                                                                                                                                          
    "startDateTimeOnLocal" : "1970-01-18 03:09:54 CST",                                                                                                                                                                                                             
    "startDateTime" : 1501794715,                                                                                                                                                                                                                                   
    "endDateTimeOnLocal" : "2017-08-11 16:57:04 CDT",                                                                                                                                                                                                               
    "endDateTime" : 1502488624000,                                                                                                                                                                                                                                  
    "isEffective" : true,                                                                                                                                                                                                                                           
    "type" : "DeviceSDT",                                                                                                                                                                                                                                           
    "deviceId" : 92,                                                                                                                                                                                                                                                
    "deviceDisplayName" : "CentOSTEMP"                                                                                                                                                                                                                              
  }