API: PHP

The example below includes a cURL library-based PHP script which performs a request and a Page containing a few API call examples based on this script; including authorization, getting a list of platforms, getting a device list by platform, creating a schedule and a notification group.

This script contains a main request function, based on a PHP cURL library, which calls API methods and displays a JSON object as a result.

CODE:
<?php
// constant Dotcom-Monitor API v.1 address
$API_URL = 'https://api.dotcom-monitor.com/config_api_v1/';
// $action - dynamic url part
// $method - HTTP method
// $data - POST data, 'null' for GET request
function Request($action, $method, $data) {
// accessing global variable
global $API_URL;
// setting request url (merging "constant" and "dynamic" part)
$ch = curl_init($API_URL . $action);
// setting HTTP method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
// return string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// igroring SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// Cookie management
curl_setopt($ch, CURLOPT_COOKIEFILE, 'D:\PhpExample\cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'D:\PhpExample\cookie.txt');
// creating request header array
$headers = array('Content-Type: application/json');
// checking if 'POST' method
if($method === 'POST' && $data != null)
{
// encode input data to json
$data_string = json_encode($data);
// setting POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
// setting content length header
array_push($headers, 'Content-Length: ' . strlen($data_string));
}
// setting headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// making request
$result = curl_exec($ch);
// closing connection
curl_close($ch);
// displaying response
echo $result;
// returning deserialized json data as object
return json_decode($result);
}
?>

Page contains examples of Authorization, listing resources, device creation and simple management of notification groups and templates.

CODE:
<!-- including request function -->
<?php include 'request.php'; ?>
<!DOCTYPE html>
<!-- css styles to make page readable -->
<head>
<style type="text/css">
.request
{
background-color: #D3DEE2;
font-weight: bold;
padding: 8px;
}
.response
{
background-color: #EFEFEF;
border: 1px solid #1D4982;
margin-bottom: 25px;
overflow: auto;
padding: 5px;
}
.monitor
{
float: left;
width: 215px;
overflow: hidden;
text-overflow: ellipsis;
height: 22px;
}
.radio_block
{
float: left;
width: 100px;
overflow: hidden;
}
.form_param
{
border-bottom: 1px solid #C0C0C0;
padding-top: 10px;
clear: both;
font-weight: bold;
overflow: hidden;
margin-bottom: 5px;
}
fieldset { background-color: #F4F8FF; }
fieldset legend {
font-weight: bold;
font-size: 1.2em;
}
</style>
</head>
<body>
<h1><?php echo $API_URL; ?></h1>
<!-- Performing Log on and displaying result -->
<div class="request">Log on</div>
<div class="response">
<?php
$credentials = array("UserName" => "your_username", "Password" => "your_password");
Request('login', "POST", $credentials); // making login request with credentials (authorization)
?>
</div>
<!-- Requesting platforms information and displaying result -->
<div class="request">Get platforms</div>
<div class="response">
<?php
// get all platforms
$result = Request('platforms', "GET", null);
// adding received data into a variable for further usage
$platforms = $result;
?>
</div>
<!-- Requesting device list by each available platform and displaying result -->
<div class="request">Devices by platform</div>
<div class="response">
<?php
// variable for any first ServerView device, if available
$device_id = 0;
// iterating through all platforms
foreach ($platforms as &$platform) {
echo '<strong>' . $platform -> Name . '</strong>:<br />';
//requesting devices for platfotm
$result = Request("devices/" . $platform -> Name, "GET", null);
echo '<br />';
// checking for ServerView platform
if ($platform -> Id == 1)
{
$max = sizeof($result);
for($i = 0; $i < $max; $i++)
// get first id and break loop
{
$device_id = $result[$i];
break;
}
}
}
echo '</div>';
if ($device_id > 0)
{
echo '<div class="request">Edit device with id: ' . $device_id . '</div>';
echo '<div class="response">Load:<br />';
// loading device by id
$device = Request("device/" . $device_id, "GET", null);
echo '</div>';
echo '<div class="response">Save:<br />';
// changing device name from "device_name" to "device_name (edited)"
$device -> Name = $device -> Name . " (edited)";
// save edited device
Request("device/" . $device_id, "POST", $device);
echo '</div>';
}
?>

<!-- before we can create device, we have to receive required device properties -->
<h2>Create device</h2>
<div class="request">Locations for ServerView</div>
<div class="response"><?php $locations = Request('locations/serverview', "GET", null); ?></div>
<div class="request">Frequencies for ServerView</div>
<div class="response"><?php $frequencies = Request('frequencies/serverview', "GET", null); ?></div>
<div class="request">Get schedulers</div>
<div class="response"><?php $schedulers = Request('schedulers', "GET", null); ?></div>
<div class="request">Get Notification Groups</div>
<div class="response"><?php $groups = Request('groups', "GET", null); ?></div>
<div class="request">Get Filters</div>
<div class="response"><?php $filters = Request('filters', "GET", null); ?></div>

<br />
<!-- listing previously requested data -->
<fieldset>
<legend>Create device</legend>
<form action="example.php?create_device=true" method="post">
<?php
echo '<div class="form_param">Locations</div>';
foreach($locations as $location) {
echo '<div class="monitor"><input type="checkbox" name="locations[]" value="' . $location -> Id . '" />' . $location -> Name . '</div>';
}
echo '<div class="form_param">Frequency</div>';
foreach($frequencies as $frequency) {
echo '<div class="radio_block"><input type="radio" name="frequency" value="' . $frequency . '" />' . $frequency . '</div>';
}
echo '<div class="form_param">Filter</div>';
foreach($filters as $filter) {
echo '<div class="radio_block"><input type="radio" name="filter" value="' . $filter . '" />' . $filter . '</div>';
}
echo '<div class="form_param">Scheduler</div>';
foreach($schedulers as $scheduler) {
echo '<div class="radio_block"><input type="radio" name="scheduler" value="' . $scheduler . '" />' . $scheduler . '</div>';
}
echo '<div class="form_param">Notifications</div>';
foreach($groups as $group) {
echo '<input type="checkbox" name="notifications[]" value="' . $group . '" />' . $group . ' - Time Shift: ';
echo '<input type="text" name="notification' . $group . '" value="10" /> min <br />';
}
echo '<div class="form_param">Device name</div>';
echo '<input type="text" name="device_name" value="" />';
echo '<div class="form_param">Notification email</div>';
echo '<input type="text" name="email" value="" />';
// POST handler for device creation
if ($_POST && !empty($_GET['create_device'])) {
$notificationGroups = array();
$locationsArray = array();
if(!empty($_POST['notifications'])) {
foreach($_POST['notifications'] as $check) {
array_push( $notificationGroups, array("Id" => intval($check), "Time_Shift_Min" => $_POST['notification' . $check]));
}
}
if(!empty($_POST['locations'])) {
foreach($_POST['locations'] as $check) {
array_push( $locationsArray, intval($check));
}
}
// creating device object to be created
$data = array(
"Avoid_Simultaneous_Checks" => true,
"False_Positive_Check" => false,
"Send_Uptime_Alert" => false,
"Platform_Id" => 1,
"Locations" => $locationsArray,
"Frequency" => intval($_POST['frequency']),
"Filter_Id" => intval($_POST['filter']),
"Scheduler_Id" => intval($_POST['scheduler']),
"Name" => $_POST['device_name'],
"Notifications" => array(
"E_Mail_Flag" => true,
"E_Mail_Address" => $_POST['email'],
"Notification_Groups" => $notificationGroups
)
);
echo '<br /><br /><div class="request">Result</div>';
echo '<div class="response">';
Request("devices?verb=PUT", "POST", $data); //creating device
echo '</div>';
}
?>
<p>
<input type="submit" value="Create device" />
</p>
</form>
</fieldset>
<h2>Create scheduler</h2>
<fieldset>
<legend>Create scheduler</legend>
<form action="example.php?create_scheduler=true" method="post">
<?php
echo '<div class="form_param">Name</div>';
echo '<input type="text" name="name" value="" />';
echo '<div class="form_param">Description</div>';
echo '<input type="text" name="description" value="" />';
if ($_POST && !empty($_GET['create_scheduler'])) {
// creating scheduler object
$data = array(
"Name" => $_POST['name'],
"Description" => $_POST['description'],
"Weekly_Intervals" => array(
array(
"Days" => array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"),
"From_Min" => 0,
"To_Min" => 1025,
"Included" => true
), array(
"Days" => array("Su", "Sa"),
"From_Min" => 0,
"To_Min" => 1139,
"Included" => false
), array(
"Days" => array("Su", "Mo", "Fr", "Sa"),
"From_Min" => 120,
"To_Min" => 240,
"Included" => false
)
),
"Date_Time_Intervals" => array(
"From" => 1358712000000,
"To" => 1358798400000
)
);
echo '<br /><br /><div class="request">Result</div>';
echo '<div class="response">';
Request("schedulers?verb=PUT", "POST", $data); //making create request
echo '</div>';
}
?>
<p>
<input type="submit" value="Create scheduler" />
</p>
</form>
</fieldset>

<h2>Create notification group</h2>
<fieldset>
<legend>Create notification group</legend>
<form action="example.php?create_group=true" method="post">
<?php
echo '<div class="form_param">Name</div>';
echo '<input type="text" name="name" value="" />';
if ($_POST && !empty($_GET['create_group'])) {
// creating notification group with requered fields
$data = array(
"Name" => $_POST['name'],
"Scheduler_Id" => 0
);
echo '<br /><br /><div class="request">Result</div>';
echo '<div class="response">';
Request("groups?verb=PUT", "POST", $data); //making request
echo '</div>';
}
?>
<p>
<input type="submit" value="Create notification group" />
</p>
</form>
</fieldset>
</body>
</html>