El ejemplo siguiente incluye un script PHP basado en la biblioteca cURL que realiza una solicitud y una página que contiene algunos ejemplos de llamadas a la API basados en este script; incluyendo la autorización, la obtención de una lista de plataformas, la obtención de una lista de dispositivos por plataforma, la creación de una programación y un grupo de notificaciones.
Este script contiene una función de solicitud principal, basada en una biblioteca cURL de PHP, que llama a los métodos de la API y muestra un objeto JSON como resultado.
CODE:
<br />
[php]<br />
<encoded_tag_open />?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 />
?<encoded_tag_closed /><br />
[/php]<br />
[php]<br />
<encoded_tag_open />?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 />
?<encoded_tag_closed /><br />
[/php]<br />
La página contiene ejemplos de autorización, listado de recursos, creación de dispositivos y gestión sencilla de grupos y plantillas de notificación.
CODE:
<br />
[php]<br />
<encoded_tag_open />!– including request function –<encoded_tag_closed /><br />
<encoded_tag_open />?php include ‘request.php’; ?<encoded_tag_closed /><br />
<encoded_tag_open />!DOCTYPE html<encoded_tag_closed /><br />
<encoded_tag_open />!– css styles to make page readable –<encoded_tag_closed /><br />
<encoded_tag_open />head<encoded_tag_closed /><br />
<encoded_tag_open />style type=”text/css”<encoded_tag_closed /><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 />
<encoded_tag_open />/style<encoded_tag_closed /><br />
<encoded_tag_open />/head<encoded_tag_closed /><br />
<encoded_tag_open />body<encoded_tag_closed /><br />
<encoded_tag_open />h1<encoded_tag_closed /><encoded_tag_open />?php echo $API_URL; ?<encoded_tag_closed /><encoded_tag_open />/h1<encoded_tag_closed /><br />
<encoded_tag_open />!– Performing Log on and displaying result –<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Log on<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><br />
<encoded_tag_open />?php<br />
$credentials = array(“UserName” =<encoded_tag_closed /> “your_username”, “Password” =<encoded_tag_closed /> “your_password”);<br />
Request(‘login’, “POST”, $credentials); // making login request with credentials (authorization)<br />
?<encoded_tag_closed /><br />
<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />!– Requesting platforms information and displaying result –<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Get platforms<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><br />
<encoded_tag_open />?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 />
?<encoded_tag_closed /><br />
<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />!– Requesting device list by each available platform and displaying result –<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Devices by platform<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><br />
<encoded_tag_open />?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 ‘<encoded_tag_open />strong<encoded_tag_closed />’ . $platform -<encoded_tag_closed /> Name . ‘<encoded_tag_open />/strong<encoded_tag_closed />:<encoded_tag_open />br /<encoded_tag_closed />’;<br />
//requesting devices for platfotm<br />
$result = Request(“devices/” . $platform -<encoded_tag_closed /> Name, “GET”, null);<br />
echo ‘<encoded_tag_open />br /<encoded_tag_closed />’;<br />
// checking for ServerView platform<br />
if ($platform -<encoded_tag_closed /> Id == 1)<br />
{<br />
$max = sizeof($result);<br />
for($i = 0; $i <encoded_tag_open /> $max; $i++)<br />
// get first id and break loop<br />
{<br />
$device_id = $result[$i];<br />
break;<br />
}<br />
}<br />
}<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
if ($device_id <encoded_tag_closed /> 0)<br />
{<br />
echo ‘<encoded_tag_open />div class=”request”<encoded_tag_closed />Edit device with id: ‘ . $device_id . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />Load:<encoded_tag_open />br /<encoded_tag_closed />’;<br />
// loading device by id<br />
$device = Request(“device/” . $device_id, “GET”, null);<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />Save:<encoded_tag_open />br /<encoded_tag_closed />’;<br />
// changing device name from “device_name” to “device_name (edited)”<br />
$device -<encoded_tag_closed /> Name = $device -<encoded_tag_closed /> Name . ” (edited)”;<br />
// save edited device<br />
Request(“device/” . $device_id, “POST”, $device);<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
?<encoded_tag_closed />
<encoded_tag_open />!– before we can create device, we have to receive required device properties –<encoded_tag_closed /><br />
<encoded_tag_open />h2<encoded_tag_closed />Create device<encoded_tag_open />/h2<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Locations for ServerView<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $locations = Request(‘locations/serverview’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Frequencies for ServerView<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $frequencies = Request(‘frequencies/serverview’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Get schedulers<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $schedulers = Request(‘schedulers’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Get Notification Groups<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $groups = Request(‘groups’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Get Filters<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $filters = Request(‘filters’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed />
<encoded_tag_open />br /<encoded_tag_closed /><br />
<encoded_tag_open />!– listing previously requested data –<encoded_tag_closed /><br />
<encoded_tag_open />fieldset<encoded_tag_closed /><br />
<encoded_tag_open />legend<encoded_tag_closed />Create device<encoded_tag_open />/legend<encoded_tag_closed /><br />
<encoded_tag_open />form action=”example.php?create_device=true” method=”post”<encoded_tag_closed /><br />
<encoded_tag_open />?php<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Locations<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($locations as $location) {<br />
echo ‘<encoded_tag_open />div class=”monitor”<encoded_tag_closed /><encoded_tag_open />input type=”checkbox” name=”locations[]” value=”‘ . $location -<encoded_tag_closed /> Id . ‘” /<encoded_tag_closed />’ . $location -<encoded_tag_closed /> Name . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Frequency<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($frequencies as $frequency) {<br />
echo ‘<encoded_tag_open />div class=”radio_block”<encoded_tag_closed /><encoded_tag_open />input type=”radio” name=”frequency” value=”‘ . $frequency . ‘” /<encoded_tag_closed />’ . $frequency . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Filter<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($filters as $filter) {<br />
echo ‘<encoded_tag_open />div class=”radio_block”<encoded_tag_closed /><encoded_tag_open />input type=”radio” name=”filter” value=”‘ . $filter . ‘” /<encoded_tag_closed />’ . $filter . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Scheduler<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($schedulers as $scheduler) {<br />
echo ‘<encoded_tag_open />div class=”radio_block”<encoded_tag_closed /><encoded_tag_open />input type=”radio” name=”scheduler” value=”‘ . $scheduler . ‘” /<encoded_tag_closed />’ . $scheduler . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Notifications<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($groups as $group) {<br />
echo ‘<encoded_tag_open />input type=”checkbox” name=”notifications[]” value=”‘ . $group . ‘” /<encoded_tag_closed />’ . $group . ‘ – Time Shift: ‘;<br />
echo ‘<encoded_tag_open />input type=”text” name=”notification’ . $group . ‘” value=”10″ /<encoded_tag_closed /> min <encoded_tag_open />br /<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Device name<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”device_name” value=”” /<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Notification email<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”email” value=”” /<encoded_tag_closed />’;<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” =<encoded_tag_closed /> intval($check), “Time_Shift_Min” =<encoded_tag_closed /> $_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” =<encoded_tag_closed /> true,<br />
“False_Positive_Check” =<encoded_tag_closed /> false,<br />
“Send_Uptime_Alert” =<encoded_tag_closed /> false,<br />
“Platform_Id” =<encoded_tag_closed /> 1,<br />
“Locations” =<encoded_tag_closed /> $locationsArray,<br />
“Frequency” =<encoded_tag_closed /> intval($_POST[‘frequency’]),<br />
“Filter_Id” =<encoded_tag_closed /> intval($_POST[‘filter’]),<br />
“Scheduler_Id” =<encoded_tag_closed /> intval($_POST[‘scheduler’]),<br />
“Name” =<encoded_tag_closed /> $_POST[‘device_name’],<br />
“Notifications” =<encoded_tag_closed /> array(<br />
“E_Mail_Flag” =<encoded_tag_closed /> true,<br />
“E_Mail_Address” =<encoded_tag_closed /> $_POST[’email’],<br />
“Notification_Groups” =<encoded_tag_closed /> $notificationGroups<br />
)<br />
);<br />
echo ‘<encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />div class=”request”<encoded_tag_closed />Result<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />’;<br />
Request(“devices?verb=PUT”, “POST”, $data); //creating device<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
?<encoded_tag_closed /><br />
<encoded_tag_open />p<encoded_tag_closed /><br />
<encoded_tag_open />input type=”submit” value=”Create device” /<encoded_tag_closed /><br />
<encoded_tag_open />/p<encoded_tag_closed /><br />
<encoded_tag_open />/form<encoded_tag_closed /><br />
<encoded_tag_open />/fieldset<encoded_tag_closed /><br />
<encoded_tag_open />h2<encoded_tag_closed />Create scheduler<encoded_tag_open />/h2<encoded_tag_closed /><br />
<encoded_tag_open />fieldset<encoded_tag_closed /><br />
<encoded_tag_open />legend<encoded_tag_closed />Create scheduler<encoded_tag_open />/legend<encoded_tag_closed /><br />
<encoded_tag_open />form action=”example.php?create_scheduler=true” method=”post”<encoded_tag_closed /><br />
<encoded_tag_open />?php<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Name<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”name” value=”” /<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Description<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”description” value=”” /<encoded_tag_closed />’;<br />
if ($_POST && !empty($_GET[‘create_scheduler’])) {<br />
// creating scheduler object<br />
$data = array(<br />
“Name” =<encoded_tag_closed /> $_POST[‘name’],<br />
“Description” =<encoded_tag_closed /> $_POST[‘description’],<br />
“Weekly_Intervals” =<encoded_tag_closed /> array(<br />
array(<br />
“Days” =<encoded_tag_closed /> array(“Su”, “Mo”, “Tu”, “We”, “Th”, “Fr”, “Sa”),<br />
“From_Min” =<encoded_tag_closed /> 0,<br />
“To_Min” =<encoded_tag_closed /> 1025,<br />
“Included” =<encoded_tag_closed /> true<br />
), array(<br />
“Days” =<encoded_tag_closed /> array(“Su”, “Sa”),<br />
“From_Min” =<encoded_tag_closed /> 0,<br />
“To_Min” =<encoded_tag_closed /> 1139,<br />
“Included” =<encoded_tag_closed /> false<br />
), array(<br />
“Days” =<encoded_tag_closed /> array(“Su”, “Mo”, “Fr”, “Sa”),<br />
“From_Min” =<encoded_tag_closed /> 120,<br />
“To_Min” =<encoded_tag_closed /> 240,<br />
“Included” =<encoded_tag_closed /> false<br />
)<br />
),<br />
“Date_Time_Intervals” =<encoded_tag_closed /> array(<br />
“From” =<encoded_tag_closed /> 1358712000000,<br />
“To” =<encoded_tag_closed /> 1358798400000<br />
)<br />
);<br />
echo ‘<encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />div class=”request”<encoded_tag_closed />Result<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />’;<br />
Request(“schedulers?verb=PUT”, “POST”, $data); //making create request<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
?<encoded_tag_closed /><br />
<encoded_tag_open />p<encoded_tag_closed /><br />
<encoded_tag_open />input type=”submit” value=”Create scheduler” /<encoded_tag_closed /><br />
<encoded_tag_open />/p<encoded_tag_closed /><br />
<encoded_tag_open />/form<encoded_tag_closed /><br />
<encoded_tag_open />/fieldset<encoded_tag_closed />
<encoded_tag_open />h2<encoded_tag_closed />Create notification group<encoded_tag_open />/h2<encoded_tag_closed /><br />
<encoded_tag_open />fieldset<encoded_tag_closed /><br />
<encoded_tag_open />legend<encoded_tag_closed />Create notification group<encoded_tag_open />/legend<encoded_tag_closed /><br />
<encoded_tag_open />form action=”example.php?create_group=true” method=”post”<encoded_tag_closed /><br />
<encoded_tag_open />?php<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Name<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”name” value=”” /<encoded_tag_closed />’;<br />
if ($_POST && !empty($_GET[‘create_group’])) {<br />
// creating notification group with requered fields<br />
$data = array(<br />
“Name” =<encoded_tag_closed /> $_POST[‘name’],<br />
“Scheduler_Id” =<encoded_tag_closed /> 0<br />
);<br />
echo ‘<encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />div class=”request”<encoded_tag_closed />Result<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />’;<br />
Request(“groups?verb=PUT”, “POST”, $data); //making request<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
?<encoded_tag_closed /><br />
<encoded_tag_open />p<encoded_tag_closed /><br />
<encoded_tag_open />input type=”submit” value=”Create notification group” /<encoded_tag_closed /><br />
<encoded_tag_open />/p<encoded_tag_closed /><br />
<encoded_tag_open />/form<encoded_tag_closed /><br />
<encoded_tag_open />/fieldset<encoded_tag_closed /><br />
<encoded_tag_open />/body<encoded_tag_closed /><br />
<encoded_tag_open />/html<encoded_tag_closed /><br />
[/php]<br />
[php]<br />
<encoded_tag_open />!– including request function –<encoded_tag_closed /><br />
<encoded_tag_open />?php include ‘request.php’; ?<encoded_tag_closed /><br />
<encoded_tag_open />!DOCTYPE html<encoded_tag_closed /><br />
<encoded_tag_open />!– css styles to make page readable –<encoded_tag_closed /><br />
<encoded_tag_open />head<encoded_tag_closed /><br />
<encoded_tag_open />style type=”text/css”<encoded_tag_closed /><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 />
<encoded_tag_open />/style<encoded_tag_closed /><br />
<encoded_tag_open />/head<encoded_tag_closed /><br />
<encoded_tag_open />body<encoded_tag_closed /><br />
<encoded_tag_open />h1<encoded_tag_closed /><encoded_tag_open />?php echo $API_URL; ?<encoded_tag_closed /><encoded_tag_open />/h1<encoded_tag_closed /><br />
<encoded_tag_open />!– Performing Log on and displaying result –<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Log on<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><br />
<encoded_tag_open />?php<br />
$credentials = array(“UserName” =<encoded_tag_closed /> “your_username”, “Password” =<encoded_tag_closed /> “your_password”);<br />
Request(‘login’, “POST”, $credentials); // making login request with credentials (authorization)<br />
?<encoded_tag_closed /><br />
<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />!– Requesting platforms information and displaying result –<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Get platforms<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><br />
<encoded_tag_open />?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 />
?<encoded_tag_closed /><br />
<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />!– Requesting device list by each available platform and displaying result –<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Devices by platform<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><br />
<encoded_tag_open />?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 ‘<encoded_tag_open />strong<encoded_tag_closed />’ . $platform -<encoded_tag_closed /> Name . ‘<encoded_tag_open />/strong<encoded_tag_closed />:<encoded_tag_open />br /<encoded_tag_closed />’;<br />
//requesting devices for platfotm<br />
$result = Request(“devices/” . $platform -<encoded_tag_closed /> Name, “GET”, null);<br />
echo ‘<encoded_tag_open />br /<encoded_tag_closed />’;<br />
// checking for ServerView platform<br />
if ($platform -<encoded_tag_closed /> Id == 1)<br />
{<br />
$max = sizeof($result);<br />
for($i = 0; $i <encoded_tag_open /> $max; $i++)<br />
// get first id and break loop<br />
{<br />
$device_id = $result[$i];<br />
break;<br />
}<br />
}<br />
}<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
if ($device_id <encoded_tag_closed /> 0)<br />
{<br />
echo ‘<encoded_tag_open />div class=”request”<encoded_tag_closed />Edit device with id: ‘ . $device_id . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />Load:<encoded_tag_open />br /<encoded_tag_closed />’;<br />
// loading device by id<br />
$device = Request(“device/” . $device_id, “GET”, null);<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />Save:<encoded_tag_open />br /<encoded_tag_closed />’;<br />
// changing device name from “device_name” to “device_name (edited)”<br />
$device -<encoded_tag_closed /> Name = $device -<encoded_tag_closed /> Name . ” (edited)”;<br />
// save edited device<br />
Request(“device/” . $device_id, “POST”, $device);<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
?<encoded_tag_closed />
<encoded_tag_open />!– before we can create device, we have to receive required device properties –<encoded_tag_closed /><br />
<encoded_tag_open />h2<encoded_tag_closed />Create device<encoded_tag_open />/h2<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Locations for ServerView<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $locations = Request(‘locations/serverview’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Frequencies for ServerView<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $frequencies = Request(‘frequencies/serverview’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Get schedulers<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $schedulers = Request(‘schedulers’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Get Notification Groups<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $groups = Request(‘groups’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”request”<encoded_tag_closed />Get Filters<encoded_tag_open />/div<encoded_tag_closed /><br />
<encoded_tag_open />div class=”response”<encoded_tag_closed /><encoded_tag_open />?php $filters = Request(‘filters’, “GET”, null); ?<encoded_tag_closed /><encoded_tag_open />/div<encoded_tag_closed />
<encoded_tag_open />br /<encoded_tag_closed /><br />
<encoded_tag_open />!– listing previously requested data –<encoded_tag_closed /><br />
<encoded_tag_open />fieldset<encoded_tag_closed /><br />
<encoded_tag_open />legend<encoded_tag_closed />Create device<encoded_tag_open />/legend<encoded_tag_closed /><br />
<encoded_tag_open />form action=”example.php?create_device=true” method=”post”<encoded_tag_closed /><br />
<encoded_tag_open />?php<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Locations<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($locations as $location) {<br />
echo ‘<encoded_tag_open />div class=”monitor”<encoded_tag_closed /><encoded_tag_open />input type=”checkbox” name=”locations[]” value=”‘ . $location -<encoded_tag_closed /> Id . ‘” /<encoded_tag_closed />’ . $location -<encoded_tag_closed /> Name . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Frequency<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($frequencies as $frequency) {<br />
echo ‘<encoded_tag_open />div class=”radio_block”<encoded_tag_closed /><encoded_tag_open />input type=”radio” name=”frequency” value=”‘ . $frequency . ‘” /<encoded_tag_closed />’ . $frequency . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Filter<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($filters as $filter) {<br />
echo ‘<encoded_tag_open />div class=”radio_block”<encoded_tag_closed /><encoded_tag_open />input type=”radio” name=”filter” value=”‘ . $filter . ‘” /<encoded_tag_closed />’ . $filter . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Scheduler<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($schedulers as $scheduler) {<br />
echo ‘<encoded_tag_open />div class=”radio_block”<encoded_tag_closed /><encoded_tag_open />input type=”radio” name=”scheduler” value=”‘ . $scheduler . ‘” /<encoded_tag_closed />’ . $scheduler . ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Notifications<encoded_tag_open />/div<encoded_tag_closed />’;<br />
foreach($groups as $group) {<br />
echo ‘<encoded_tag_open />input type=”checkbox” name=”notifications[]” value=”‘ . $group . ‘” /<encoded_tag_closed />’ . $group . ‘ – Time Shift: ‘;<br />
echo ‘<encoded_tag_open />input type=”text” name=”notification’ . $group . ‘” value=”10″ /<encoded_tag_closed /> min <encoded_tag_open />br /<encoded_tag_closed />’;<br />
}<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Device name<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”device_name” value=”” /<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Notification email<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”email” value=”” /<encoded_tag_closed />’;<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” =<encoded_tag_closed /> intval($check), “Time_Shift_Min” =<encoded_tag_closed /> $_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” =<encoded_tag_closed /> true,<br />
“False_Positive_Check” =<encoded_tag_closed /> false,<br />
“Send_Uptime_Alert” =<encoded_tag_closed /> false,<br />
“Platform_Id” =<encoded_tag_closed /> 1,<br />
“Locations” =<encoded_tag_closed /> $locationsArray,<br />
“Frequency” =<encoded_tag_closed /> intval($_POST[‘frequency’]),<br />
“Filter_Id” =<encoded_tag_closed /> intval($_POST[‘filter’]),<br />
“Scheduler_Id” =<encoded_tag_closed /> intval($_POST[‘scheduler’]),<br />
“Name” =<encoded_tag_closed /> $_POST[‘device_name’],<br />
“Notifications” =<encoded_tag_closed /> array(<br />
“E_Mail_Flag” =<encoded_tag_closed /> true,<br />
“E_Mail_Address” =<encoded_tag_closed /> $_POST[’email’],<br />
“Notification_Groups” =<encoded_tag_closed /> $notificationGroups<br />
)<br />
);<br />
echo ‘<encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />div class=”request”<encoded_tag_closed />Result<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />’;<br />
Request(“devices?verb=PUT”, “POST”, $data); //creating device<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
?<encoded_tag_closed /><br />
<encoded_tag_open />p<encoded_tag_closed /><br />
<encoded_tag_open />input type=”submit” value=”Create device” /<encoded_tag_closed /><br />
<encoded_tag_open />/p<encoded_tag_closed /><br />
<encoded_tag_open />/form<encoded_tag_closed /><br />
<encoded_tag_open />/fieldset<encoded_tag_closed /><br />
<encoded_tag_open />h2<encoded_tag_closed />Create scheduler<encoded_tag_open />/h2<encoded_tag_closed /><br />
<encoded_tag_open />fieldset<encoded_tag_closed /><br />
<encoded_tag_open />legend<encoded_tag_closed />Create scheduler<encoded_tag_open />/legend<encoded_tag_closed /><br />
<encoded_tag_open />form action=”example.php?create_scheduler=true” method=”post”<encoded_tag_closed /><br />
<encoded_tag_open />?php<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Name<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”name” value=”” /<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Description<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”description” value=”” /<encoded_tag_closed />’;<br />
if ($_POST && !empty($_GET[‘create_scheduler’])) {<br />
// creating scheduler object<br />
$data = array(<br />
“Name” =<encoded_tag_closed /> $_POST[‘name’],<br />
“Description” =<encoded_tag_closed /> $_POST[‘description’],<br />
“Weekly_Intervals” =<encoded_tag_closed /> array(<br />
array(<br />
“Days” =<encoded_tag_closed /> array(“Su”, “Mo”, “Tu”, “We”, “Th”, “Fr”, “Sa”),<br />
“From_Min” =<encoded_tag_closed /> 0,<br />
“To_Min” =<encoded_tag_closed /> 1025,<br />
“Included” =<encoded_tag_closed /> true<br />
), array(<br />
“Days” =<encoded_tag_closed /> array(“Su”, “Sa”),<br />
“From_Min” =<encoded_tag_closed /> 0,<br />
“To_Min” =<encoded_tag_closed /> 1139,<br />
“Included” =<encoded_tag_closed /> false<br />
), array(<br />
“Days” =<encoded_tag_closed /> array(“Su”, “Mo”, “Fr”, “Sa”),<br />
“From_Min” =<encoded_tag_closed /> 120,<br />
“To_Min” =<encoded_tag_closed /> 240,<br />
“Included” =<encoded_tag_closed /> false<br />
)<br />
),<br />
“Date_Time_Intervals” =<encoded_tag_closed /> array(<br />
“From” =<encoded_tag_closed /> 1358712000000,<br />
“To” =<encoded_tag_closed /> 1358798400000<br />
)<br />
);<br />
echo ‘<encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />div class=”request”<encoded_tag_closed />Result<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />’;<br />
Request(“schedulers?verb=PUT”, “POST”, $data); //making create request<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
?<encoded_tag_closed /><br />
<encoded_tag_open />p<encoded_tag_closed /><br />
<encoded_tag_open />input type=”submit” value=”Create scheduler” /<encoded_tag_closed /><br />
<encoded_tag_open />/p<encoded_tag_closed /><br />
<encoded_tag_open />/form<encoded_tag_closed /><br />
<encoded_tag_open />/fieldset<encoded_tag_closed />
<encoded_tag_open />h2<encoded_tag_closed />Create notification group<encoded_tag_open />/h2<encoded_tag_closed /><br />
<encoded_tag_open />fieldset<encoded_tag_closed /><br />
<encoded_tag_open />legend<encoded_tag_closed />Create notification group<encoded_tag_open />/legend<encoded_tag_closed /><br />
<encoded_tag_open />form action=”example.php?create_group=true” method=”post”<encoded_tag_closed /><br />
<encoded_tag_open />?php<br />
echo ‘<encoded_tag_open />div class=”form_param”<encoded_tag_closed />Name<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />input type=”text” name=”name” value=”” /<encoded_tag_closed />’;<br />
if ($_POST && !empty($_GET[‘create_group’])) {<br />
// creating notification group with requered fields<br />
$data = array(<br />
“Name” =<encoded_tag_closed /> $_POST[‘name’],<br />
“Scheduler_Id” =<encoded_tag_closed /> 0<br />
);<br />
echo ‘<encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />br /<encoded_tag_closed /><encoded_tag_open />div class=”request”<encoded_tag_closed />Result<encoded_tag_open />/div<encoded_tag_closed />’;<br />
echo ‘<encoded_tag_open />div class=”response”<encoded_tag_closed />’;<br />
Request(“groups?verb=PUT”, “POST”, $data); //making request<br />
echo ‘<encoded_tag_open />/div<encoded_tag_closed />’;<br />
}<br />
?<encoded_tag_closed /><br />
<encoded_tag_open />p<encoded_tag_closed /><br />
<encoded_tag_open />input type=”submit” value=”Create notification group” /<encoded_tag_closed /><br />
<encoded_tag_open />/p<encoded_tag_closed /><br />
<encoded_tag_open />/form<encoded_tag_closed /><br />
<encoded_tag_open />/fieldset<encoded_tag_closed /><br />
<encoded_tag_open />/body<encoded_tag_closed /><br />
<encoded_tag_open />/html<encoded_tag_closed /><br />
[/php]<br />