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:

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

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

CODE:

<br />
<!-- including request function --><br />
<?php include 'request.php'; ?><br />
<!DOCTYPE html><br />
<!-- css styles to make page readable --><br />
<head><br />
<style type="text/css"><br />
.request<br />
{<br />
background-color: #D3DEE2;<br />
font-weight: bold;<br />
padding: 8px;<br />
}<br />
.response<br />
{<br />
background-color: #EFEFEF;<br />
border: 1px solid #1D4982;<br />
margin-bottom: 25px;<br />
overflow: auto;<br />
padding: 5px;<br />
}<br />
.monitor<br />
{<br />
float: left;<br />
width: 215px;<br />
overflow: hidden;<br />
text-overflow: ellipsis;<br />
height: 22px;<br />
}<br />
.radio_block<br />
{<br />
float: left;<br />
width: 100px;<br />
overflow: hidden;<br />
}<br />
.form_param<br />
{<br />
border-bottom: 1px solid #C0C0C0;<br />
padding-top: 10px;<br />
clear: both;<br />
font-weight: bold;<br />
overflow: hidden;<br />
margin-bottom: 5px;<br />
}<br />
fieldset { background-color: #F4F8FF; }<br />
fieldset legend {<br />
font-weight: bold;<br />
font-size: 1.2em;<br />
}<br />
</style><br />
</head><br />
<body><br />
<h1><?php echo $API_URL; ?></h1><br />
<!-- Performing Log on and displaying result --><br />
<div class="request">Log on</div><br />
<div class="response"><br />
<?php<br />
$credentials = array("UserName" => "your_username", "Password" => "your_password");<br />
Request('login', "POST", $credentials); // making login request with credentials (authorization)<br />
?><br />
</div><br />
<!-- Requesting platforms information and displaying result --><br />
<div class="request">Get platforms</div><br />
<div class="response"><br />
<?php<br />
// get all platforms<br />
$result = Request('platforms', "GET", null);<br />
// adding received data into a variable for further usage<br />
$platforms = $result;<br />
?><br />
</div><br />
<!-- Requesting device list by each available platform and displaying result --><br />
<div class="request">Devices by platform</div><br />
<div class="response"><br />
<?php<br />
// variable for any first ServerView device, if available<br />
$device_id = 0;<br />
// iterating through all platforms<br />
foreach ($platforms as &$platform) {<br />
echo '<strong>' . $platform -> Name . '</strong>:<br />';<br />
//requesting devices for platfotm<br />
$result = Request("devices/" . $platform -> Name, "GET", null);<br />
echo '<br />';<br />
// checking for ServerView platform<br />
if ($platform -> Id == 1)<br />
{<br />
$max = sizeof($result);<br />
for($i = 0; $i < $max; $i++)<br />
// get first id and break loop<br />
{<br />
$device_id = $result[$i];<br />
break;<br />
}<br />
}<br />
}<br />
echo '</div>';<br />
if ($device_id > 0)<br />
{<br />
echo '<div class="request">Edit device with id: ' . $device_id . '</div>';<br />
echo '<div class="response">Load:<br />';<br />
// loading device by id<br />
$device = Request("device/" . $device_id, "GET", null);<br />
echo '</div>';<br />
echo '<div class="response">Save:<br />';<br />
// changing device name from "device_name" to "device_name (edited)"<br />
$device -> Name = $device -> Name . " (edited)";<br />
// save edited device<br />
Request("device/" . $device_id, "POST", $device);<br />
echo '</div>';<br />
}<br />
?></p>
<p><!-- before we can create device, we have to receive required device properties --><br />
<h2>Create device</h2><br />
<div class="request">Locations for ServerView</div><br />
<div class="response"><?php $locations = Request('locations/serverview', "GET", null); ?></div><br />
<div class="request">Frequencies for ServerView</div><br />
<div class="response"><?php $frequencies = Request('frequencies/serverview', "GET", null); ?></div><br />
<div class="request">Get schedulers</div><br />
<div class="response"><?php $schedulers = Request('schedulers', "GET", null); ?></div><br />
<div class="request">Get Notification Groups</div><br />
<div class="response"><?php $groups = Request('groups', "GET", null); ?></div><br />
<div class="request">Get Filters</div><br />
<div class="response"><?php $filters = Request('filters', "GET", null); ?></div></p>
<p><br /><br />
<!-- listing previously requested data --><br />
<fieldset><br />
<legend>Create device</legend><br />
<form action="example.php?create_device=true" method="post"><br />
<?php<br />
echo '<div class="form_param">Locations</div>';<br />
foreach($locations as $location) {<br />
echo '<div class="monitor"><input type="checkbox" name="locations[]" value="' . $location -> Id . '" />' . $location -> Name . '</div>';<br />
}<br />
echo '<div class="form_param">Frequency</div>';<br />
foreach($frequencies as $frequency) {<br />
echo '<div class="radio_block"><input type="radio" name="frequency" value="' . $frequency . '" />' . $frequency . '</div>';<br />
}<br />
echo '<div class="form_param">Filter</div>';<br />
foreach($filters as $filter) {<br />
echo '<div class="radio_block"><input type="radio" name="filter" value="' . $filter . '" />' . $filter . '</div>';<br />
}<br />
echo '<div class="form_param">Scheduler</div>';<br />
foreach($schedulers as $scheduler) {<br />
echo '<div class="radio_block"><input type="radio" name="scheduler" value="' . $scheduler . '" />' . $scheduler . '</div>';<br />
}<br />
echo '<div class="form_param">Notifications</div>';<br />
foreach($groups as $group) {<br />
echo '<input type="checkbox" name="notifications[]" value="' . $group . '" />' . $group . ' - Time Shift: ';<br />
echo '<input type="text" name="notification' . $group . '" value="10" /> min <br />';<br />
}<br />
echo '<div class="form_param">Device name</div>';<br />
echo '<input type="text" name="device_name" value="" />';<br />
echo '<div class="form_param">Notification email</div>';<br />
echo '<input type="text" name="email" value="" />';<br />
// POST handler for device creation<br />
if ($_POST && !empty($_GET['create_device'])) {<br />
$notificationGroups = array();<br />
$locationsArray = array();<br />
if(!empty($_POST['notifications'])) {<br />
foreach($_POST['notifications'] as $check) {<br />
array_push( $notificationGroups, array("Id" => intval($check), "Time_Shift_Min" => $_POST['notification' . $check]));<br />
}<br />
}<br />
if(!empty($_POST['locations'])) {<br />
foreach($_POST['locations'] as $check) {<br />
array_push( $locationsArray, intval($check));<br />
}<br />
}<br />
// creating device object to be created<br />
$data = array(<br />
"Avoid_Simultaneous_Checks" => true,<br />
"False_Positive_Check" => false,<br />
"Send_Uptime_Alert" => false,<br />
"Platform_Id" => 1,<br />
"Locations" => $locationsArray,<br />
"Frequency" => intval($_POST['frequency']),<br />
"Filter_Id" => intval($_POST['filter']),<br />
"Scheduler_Id" => intval($_POST['scheduler']),<br />
"Name" => $_POST['device_name'],<br />
"Notifications" => array(<br />
"E_Mail_Flag" => true,<br />
"E_Mail_Address" => $_POST['email'],<br />
"Notification_Groups" => $notificationGroups<br />
)<br />
);<br />
echo '<br /><br /><div class="request">Result</div>';<br />
echo '<div class="response">';<br />
Request("devices?verb=PUT", "POST", $data); //creating device<br />
echo '</div>';<br />
}<br />
?><br />
<p><br />
<input type="submit" value="Create device" /><br />
</p><br />
</form><br />
</fieldset><br />
<h2>Create scheduler</h2><br />
<fieldset><br />
<legend>Create scheduler</legend><br />
<form action="example.php?create_scheduler=true" method="post"><br />
<?php<br />
echo '<div class="form_param">Name</div>';<br />
echo '<input type="text" name="name" value="" />';<br />
echo '<div class="form_param">Description</div>';<br />
echo '<input type="text" name="description" value="" />';<br />
if ($_POST && !empty($_GET['create_scheduler'])) {<br />
// creating scheduler object<br />
$data = array(<br />
"Name" => $_POST['name'],<br />
"Description" => $_POST['description'],<br />
"Weekly_Intervals" => array(<br />
array(<br />
"Days" => array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"),<br />
"From_Min" => 0,<br />
"To_Min" => 1025,<br />
"Included" => true<br />
), array(<br />
"Days" => array("Su", "Sa"),<br />
"From_Min" => 0,<br />
"To_Min" => 1139,<br />
"Included" => false<br />
), array(<br />
"Days" => array("Su", "Mo", "Fr", "Sa"),<br />
"From_Min" => 120,<br />
"To_Min" => 240,<br />
"Included" => false<br />
)<br />
),<br />
"Date_Time_Intervals" => array(<br />
"From" => 1358712000000,<br />
"To" => 1358798400000<br />
)<br />
);<br />
echo '<br /><br /><div class="request">Result</div>';<br />
echo '<div class="response">';<br />
Request("schedulers?verb=PUT", "POST", $data); //making create request<br />
echo '</div>';<br />
}<br />
?><br />
<p><br />
<input type="submit" value="Create scheduler" /><br />
</p><br />
</form><br />
</fieldset></p>
<p><h2>Create notification group</h2><br />
<fieldset><br />
<legend>Create notification group</legend><br />
<form action="example.php?create_group=true" method="post"><br />
<?php<br />
echo '<div class="form_param">Name</div>';<br />
echo '<input type="text" name="name" value="" />';<br />
if ($_POST && !empty($_GET['create_group'])) {<br />
// creating notification group with requered fields<br />
$data = array(<br />
"Name" => $_POST['name'],<br />
"Scheduler_Id" => 0<br />
);<br />
echo '<br /><br /><div class="request">Result</div>';<br />
echo '<div class="response">';<br />
Request("groups?verb=PUT", "POST", $data); //making request<br />
echo '</div>';<br />
}<br />
?><br />
<p><br />
<input type="submit" value="Create notification group" /><br />
</p><br />
</form><br />
</fieldset><br />
</body><br />
</html><br />