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"
}
9 changes: 5 additions & 4 deletions examples/getChecksAsJSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@
*/
/**
* Returns a list of all checks on an account as a JSON string
*
*
* @package php-pingdom
* @subpackage examples
*/

DEFINE('PINGDOM_USR', 'username@email.com');
DEFINE('PINGDOM_PWD', 'MyReallyStrongPassword');
DEFINE('PINGDOM_USR', 'username@email.com');
DEFINE('PINGDOM_PWD', 'MyReallyStrongPassword');
DEFINE('PINGDOM_APIKEY', 'myapikey');

require_once dirname(__FILE__).'/../src/Pingdom/Autoload.php';
Pingdom_Autoload::register();

$api = new Pingdom_API(PINGDOM_USR, PINGDOM_PWD);
$api = new Pingdom_API(PINGDOM_USR, PINGDOM_PWD, PINGDOM_APIKEY);
try {
$resp = $api->getChecks();
echo json_encode($resp);
Expand Down
162 changes: 121 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,102 @@ 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);
}

/**
* Return a list of raw test results for a specified check
* @param Int $checkId
* @param Int $from Start of period. Format is UNIX timestamp default: 1 day prior to 'to'
* @param Int $to End of period. Format is UNIX timestamp default: current time
* @param String $probes Filter to only show results from a list of probes. Format is a comma separated list of probe identifiers default: all probes
* @param String $status Filter to only show results with specified statuses. Format is a comma separated list of (down, up, unconfirmed, unknown) default: all statuses
* @param Int $limit Number of results to show (Will be set to 1000 if the provided value is greater than 1000) default: 1000
* @param Int $offset Number of results to skip (Max value is 43200) default: 0
* @param Bool $includeanalysis Attach available root cause analysis identifiers to corresponding results default: false
* @param Int $maxresponse Maximum response time (ms). If set, specified interval must not be larger than 31 days.
* @param Int $minresponse Minimum response time (ms). If set, specified interval must not be larger than 31 days.
* @return type
*/
public function getRawCheckResults(
$checkId,
$from = null,
$to = null,
$probes = null,
$status = null,
$limit = null,
$offset = null,
$includeanalysis = null,
$maxresponse = null,
$minresponse = null )
{
$url = "/results/$checkId?";

if ($from !== null ) $url .= "from=$from&";
if ($to !== null ) $url .= "to=$to&";
if ($probes !== null ) $url .= "probes=$probes&";
if ($status !== null ) $url .= "status=$status&";
if ($limit !== null ) $url .= "limit=$limit&";
if ($offset !== null ) $url .= "offset=$offset&";
if ($includeanalysis !== null ) $url .= "includeanalysis=$includeanalysis&";
if ($maxresponse !== null ) $url .= "maxresponse=$maxresponse&";
if ($minresponse !== null ) $url .= "minresponse=$minresponse&";

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


/**
* Returns a detailed description of a specified check
Expand All @@ -223,7 +303,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 +317,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