Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
URv2
<?php
/**
* @copyright Copyright (C) 2017, 2018, 2019 Blue Flame Digital Solutions Limited / Phil Taylor. All rights reserved.
* @author Phil Taylor <phil@phil-taylor.com>
*
* @see https://github.com/PhilETaylor/maintain.myjoomla.com
*
* @license Commercial License - Not For Distribution.
*/
namespace MyJoomla\Service;
/*
* Class UptimeRobot.
*/
use GuzzleHttp\Exception\GuzzleException;
/**
* Class UptimeRobot.
*/
class UptimeRobot
{
// see https://uptimerobot.com/api getMonitors, deleteMonitor and newMonitor
const ALERT_CONTACT_ID = 52138;
/**
* @var Guzzle
*/
private $client;
/**
* @var
*/
private $apikey;
/**
* UptimeRobot constructor.
*
* @param $apikey
* @param Guzzle $client
*/
public function __construct($apikey, Guzzle $client)
{
$this->apikey = $apikey;
$this->client = $client;
}
/**
* @param $id
*
* @return string
*
* @throws GuzzleException
*/
public function deleteMonitor($id)
{
$res = $this->client->request('POST', 'https://api.uptimerobot.com/v2/deleteMonitor', [
'form_params' => [
'api_key' => $this->apikey,
'format' => 'json',
'id' => $id,
],
]);
return $res->getBody()->getContents();
}
/**
* @param $friendlyname
* @param $url
*
* @return string
*
* @throws GuzzleException
*/
public function newMonitor($friendlyname, $url)
{
$res = $this->client->request('POST', 'https://api.uptimerobot.com/v2/newMonitor', [
'form_params' => [
'friendly_name' => $friendlyname,
'api_key' => $this->apikey,
'alert_contacts' => self::ALERT_CONTACT_ID,
'format' => 'json',
'url' => $url,
'type' => 1,
],
]);
return $res->getBody()->getContents();
}
/**
* @param array $monitors
* @param int $limit
* @param int $offset
* @param int $logsAndResponseTimes
*
* @return string
*
* @throws GuzzleException
*/
public function getMonitors($monitors = [], $limit = 50, $offset = 0, $logsAndResponseTimes = 1)
{
$res = $this->client->request('POST', 'https://api.uptimerobot.com/v2/getMonitors', [
'form_params' => [
'api_key' => $this->apikey,
'monitors' => $monitors,
'custom_uptime_ratios' => '7-30-365',
'all_time_uptime_ratio' => 1,
'logs' => $logsAndResponseTimes,
'response_times' => $logsAndResponseTimes,
'limit' => $limit,
'offset' => $offset,
],
]);
return $res->getBody()->getContents();
}
/**
* @param array $monitors
* @param int $limit
* @param int $offset
* @param int $logsAndResponseTimes
*
* @return string
*
* @throws GuzzleException
*/
public function getCrapMonitors($monitors = [], $limit = 50, $offset = 0, $logsAndResponseTimes = 1)
{
$res = $this->client->request('POST', 'https://api.uptimerobot.com/v2/getMonitors', [
'form_params' => [
'api_key' => $this->apikey,
'monitors' => $monitors,
'statuses' => '0-1-8-9',
'custom_uptime_ratios' => 1,
'all_time_uptime_ratio' => 1,
'logs' => $logsAndResponseTimes,
'response_times' => $logsAndResponseTimes,
'limit' => $limit,
'offset' => $offset,
],
]);
return $res->getBody()->getContents();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.