Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "dixcart/php-pingdom"
}
121 changes: 80 additions & 41 deletions src/Pingdom/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,24 @@ class Pingdom_API {
const METHOD_DELETE = 3;
const METHOD_PUT = 4;

const APP_KEY = "rbh7purydqixtjn7ngpyxrjwjr4sqnna";
const API_ADDR = "https://api.pingdom.com/api/2.0";
const API_ADDR = 'https://api.pingdom.com/api/2.0';

private $_apiUser;
private $_apiPass;
private $_acceptGzip;
private $_lastResponse;
private $_lastStatus;
protected $_apiUser;
protected $_apiPass;
protected $_apiKey;
protected $_acceptGzip;
protected $_lastResponse;
protected $_lastStatus;

function __construct($apiUser, $apiPass, $acceptGzip = true)
function __construct($apiUser, $apiPass, $apiKey, $acceptGzip = true)
{
if(!$apiUser || !$apiPass) {
throw new Exception('Username/Password required');
}

$this->_apiUser = $apiUser;
$this->_apiPass = $apiPass;
$this->_apiKey = $apiKey;
$this->_acceptGzip = $acceptGzip;
}

Expand All @@ -65,34 +66,34 @@ function _doRequest($path, $data = null, $method = null)

//Set app-key header
$headers = array(
sprintf("%s: %s", "App-Key", self::APP_KEY)
sprintf('%s: %s', 'App-Key', $this->_apiKey)
);

//Enable GZip encoding
if ($this->_acceptGzip) curl_setopt($curl, CURLOPT_ENCODING, "gzip");
if ($this->_acceptGzip) curl_setopt($curl, CURLOPT_ENCODING, 'gzip');

//Set URL
curl_setopt($curl, CURLOPT_URL, self::API_ADDR.$path);


switch($method) {
case self::METHOD_POST:
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case self::METHOD_DELETE:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case self::METHOD_PUT:
curl_setopt($curl, CURLOPT_PUT, true);
$putData = tmpfile();
fwrite($putData, $data);
fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($data));
break;
curl_setopt($curl, CURLOPT_PUT, true);
$putData = tmpfile();
fwrite($putData, $data);
fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($data));
break;
default:
break;
break;
}

//Set headers
Expand All @@ -106,8 +107,8 @@ function _doRequest($path, $data = null, $method = null)

//Handle response and status codes
$curlinfo = curl_getinfo($curl);
if (!empty($curlinfo["http_code"])) {
$this->_lastStatus = $curlinfo["http_code"];
if (!empty($curlinfo['http_code'])) {
$this->_lastStatus = $curlinfo['http_code'];
switch ($this->_lastStatus) {
case '200':
//All ok
Expand All @@ -122,7 +123,7 @@ function _doRequest($path, $data = null, $method = null)
default:
//There was an error
$err = json_decode($this->_lastResponse, true);
throw new Exception('There was an error: ' . $err['error']['statuscode'] . " - " . $err['error']['statusdesc'] . ": " . $err['error']['errormessage']);
throw new Exception('There was an error: ' . $err['error']['statuscode'] . ' - ' . $err['error']['statusdesc'] . (isset($err['error']['errormessage']) ? ': ' . $err['error']['errormessage'] : ''));
break;
}
}
Expand All @@ -148,13 +149,13 @@ public function getActions($limit = 100, $offset = 0, $from = null, $to = null,
{
if ($offset > 25000) throw new Exception('Limit set too high');

$url = "/actions/?limit=" . $limit . "&offset=" . $offset;
if ($from != null) $usr += "&from=" . $from;
if ($to != null) $usr += "&to=" . $to;
if ($checkIds != null) $usr += "&checkids=" . $checkIds;
if ($contactIds != null) $usr += "&contactids=" . $contactIds;
if ($status != null) $usr += "&status=" . $status;
if ($via != null) $usr += "&via=" . $via;
$url = '/actions/?limit=' . $limit . '&offset=' . $offset;
if ($from != null) $url .= '&from=' . $from;
if ($to != null) $url .= '&to=' . $to;
if ($checkIds != null) $url .= '&checkids=' . $checkIds;
if ($contactIds != null) $url .= '&contactids=' . $contactIds;
if ($status != null) $url .= '&status=' . $status;
if ($via != null) $url .= '&via=' . $via;

return $this->_doRequest($url);
}
Expand All @@ -168,7 +169,7 @@ public function getActions($limit = 100, $offset = 0, $from = null, $to = null,
*/
public function getError($checkId, $limit = 100, $offset = 0)
{
$url = "/analysis/" . $checkId . "/?limit=" . $limit . "&offset=" . $offset;
$url = '/analysis/' . $checkId . '/?limit=' . $limit . '&offset=' . $offset;
return $this->_doRequest($url);
}

Expand All @@ -182,7 +183,7 @@ public function getError($checkId, $limit = 100, $offset = 0)
*/
public function getRawAnalysis($checkId, $analysisId)
{
$url = "/analysis/" . $checkId . "/" . $analysisId;
$url = '/analysis/' . $checkId . '/' . $analysisId;
return $this->_doRequest($url);
}

Expand All @@ -197,23 +198,61 @@ public function getChecks($limit = 500, $offset = 0)
{
if ($offset > 25000) throw new Exception('Limit set too high');

$url = "/checks/?limit=" . $limit . "&offset=" . $offset;
$url = '/checks/?limit=' . $limit . '&offset=' . $offset;
return $this->_doRequest($url);
}

public function getProbes($limit = 500, $offset = 0, $onlyactive="false")
public function getProbes($limit = 500, $offset = 0, $onlyactive='false')
{
if ($offset > 25000) throw new Exception('Limit set too high');
$url = "/probes?limit=" . $limit . "&offset=" . $offset. "&onlyactive=" . $onlyactive;
$url = '/probes?limit=' . $limit . '&offset=' . $offset. '&onlyactive=' . $onlyactive;
return $this->_doRequest($url);
}

public function getTraceroute($target, $probeid = 0)
{
$url = "/traceroute". "?host=" . $target. "&probeid=" . $probeid;
$url = '/traceroute'. '?host=' . $target. '&probeid=' . $probeid;
return $this->_doRequest($url);
}

public function getSummaryAverage($checkId, $from = 0, $to = null, $includeuptime = 'false', $bycountry = 'false', $byprobe = 'false')
{
$url = "/summary.average/$checkId?";

if ($from !== 0) $url .= "from=$from&";
if ($to !== null) $url .= "to=$to";

$url .= "&includeuptime=$includeuptime&bycountry=$bycountry"; //&byprobe=$byprobe";

return $this->_doRequest($url);
}

public function getSummaryPerformance($checkId, $from = null, $to = null, $resolution = 'hour', $includeuptime = 'false', $probes = null, $order = 'asc')
{
$url = "/summary.performance/$checkId?";

if ($from !== null) $url .= "from=$from&";
if ($to !== null) $url .= "to=$to&";
if ($probes !== null) $url .= "probes=$probes&";
if ($includeuptime !== 'false') $url .= "includeuptime=$includeuptime&";

$url .= "resolution=$resolution&order=$order";

return $this->_doRequest($url);
}

public function getSummaryHoursOfDay($checkId, $from = null, $to = null, $probes = null, $uselocaltime = 'false')
{
$url = "/summary.hoursofday/$checkId?";

if ($from !== null) $url .= "from=$from&";
if ($to !== null) $url .= "to=$to&";
if ($probes !== null) $url .= "probes=$probes&";
if ($uselocaltime !== 'false') $url .= "uselocaltime=$uselocaltime&";

return $this->_doRequest($url);
}


/**
* Returns a detailed description of a specified check
Expand All @@ -223,7 +262,7 @@ public function getTraceroute($target, $probeid = 0)
*/
public function getCheck($checkId)
{
$url = "/checks/" . $checkId;
$url = '/checks/' . $checkId;
return $this->_doRequest($url);

}
Expand All @@ -237,7 +276,7 @@ public function getCheck($checkId)
*/
public function addCheck($check)
{
$url = "/checks";
$url = '/checks';
$postData = $check->_prepData();
return $this->_doRequest($url, $postData, self::METHOD_POST);
}
Expand Down