Below are examples of how you can use the Pingdom API using PHP5. These examples show you how to connect and use the basic functions to integrate Pingdom into your own web site.
Download all the PHP examples below in one file
Code:
// {{{ Definitions of constants
//This is a URL of our web service server
define('SERVER_URL', 'https://ws.pingdom.com/soap/PingdomAPI.wsdl');
//You should assign here your e-mail address that you use to login on Pingdom Panel
define('USERNAME', '');
//You should assign here password that you use to login on Pingdom Panel
define('PASSWORD', '');
//You should assign here your Pingdom API key, you can get it on Pingdom API page when you login on Pingdom Panel
define('PINGDOM_API_KEY', '');
//Pingdom API status code when everything wen well
define('PINGDOM_API_STATUS_OK', 0);
//You should assign here name of the check for which you want downtimes summary
define('CHECK_NAME', '');
//You should assign here time period in seconds for getting a downs
define('TIME_PERIOD', 604799); // 7 days - 1 second
// }}}
// {{{ Creation of soap client
$client = new SoapClient(
SERVER_URL,
array(
'trace' => 1,
'exceptions' => 0
)
);
// }}}
/*
1. How to initialize and connect to Pingdom
*/
// {{{ Log on our web server
$login_data->username = USERNAME;
$login_data->password = PASSWORD;
//Pingdom API function, see details on http://www.pingdom.com/services/api-documentation/
$login_response = $client->Auth_login(PINGDOM_API_KEY, $login_data);
//Check if everything is OK
if (PINGDOM_API_STATUS_OK != $login_response->status)
{
exit('Unable to login');
}
//Without this value you wont be able to call any other Pingdom API function
$sessionId = $login_response->sessionId;
// }}}
//If you get here you are logged on Pingdom server, and you can use our other functions with $login_response->sessionId
echo '1. How to initialize and connect to Pingdom';
echo '<BR />';
Code:
/*
2. How to get a list of your checks
*/
// {{{ Get list of your checks
$get_list_response = $client->Check_getList(PINGDOM_API_KEY, $sessionId);
//Check if everything is OK
if (PINGDOM_API_STATUS_OK != $get_list_response->status)
{
exit('Error occurred while trying to get list of checks');
}
$list_of_checks = $get_list_response->checkNames;
// }}}
echo '2. How to get a list of your checks';
echo '<BR />';
foreach ($list_of_checks as $check_name)
{
echo $check_name . "<BR />";
}
echo '<BR />';
Example output:
www.myownwebsite.com/store/
Main web server
Backup web server
Code:
/*
3. How to get the current status for a check.
*/
// {{{ Get list of statuses for your checks
$current_states_response = $client->Report_getCurrentStates(PINGDOM_API_KEY, $sessionId);
//Check if everything is OK
if (PINGDOM_API_STATUS_OK != $current_states_response->status)
{
exit('Error occurred while trying to get list of statuses for your checks');
}
$list_of_states = $current_states_response->currentStates;
// }}}
echo '3. How to get the current status for a check.';
echo '<BR />';
foreach ($list_of_states as $check_state)
{
echo $check_state->checkState.'<BR />';
echo $check_state->checkName.'<BR />';
echo '<BR />';
}
echo '<BR />';
Example output:
CHECK_UP
www.myownwebsite.com/store/
CHECK_UP
Main web server
CHECK_DOWN
Backup web server
Code:
/*
4. How to get the last downtime for a check.
*/
// {{{ Get list of yours checks last downs
$last_down_response = $client->Report_getLastDowns(PINGDOM_API_KEY, $sessionId);
//Check if everything is OK
if (PINGDOM_API_STATUS_OK != $last_down_response->status)
{
exit('Error occurred while trying to get list of yours checks last downs');
}
$last_downtimes = $last_down_response->lastDowns;
// }}}
echo '4. How to get the last downtime for a check.';
echo '<BR />';
foreach ($last_downtimes as $down)
{
echo $down->lastDown.'<BR />';
echo $down->checkName.'<BR />';
echo '<BR />';
}
echo '<BR />';
Example output:
2007-03-04T19:07:10-06:00
www.myownwebsite.com/store/
2007-03-09T00:41:15-06:00
Main web server
2007-03-04T19:07:08-06:00
Backup web server
Code:
/*
5. How to get a list of downtimes for a check.
*/
// {{{ Get list of downtimes for check whit specific name
$get_downtimes_request->checkName = CHECK_NAME;
$get_downtimes_request->from = time() - TIME_PERIOD;
$get_downtimes_request->to = time();
$get_downtimes_request->resolution = 'DAILY';
$get_downtimes_response = $client->Report_getDowntimes(PINGDOM_API_KEY, $sessionId, $get_downtimes_request);
//Check if everything is OK
if (PINGDOM_API_STATUS_OK != $last_down_response->status)
{
exit('Error occurred while trying to get list of downtimes');
}
$list_of_downtimes = $get_downtimes_response->downtimesArray;
// }}}
echo '5. How to get a list of downtimes for a check.';
echo '<BR />';
echo 'Check name: '.CHECK_NAME;
echo '<BR />';
echo '<BR />';
foreach ($list_of_downtimes as $down)
{
echo 'From: '.$down->from.'<BR />';
echo 'To: '.$down->to.'<BR />';
echo 'Duration: '.$down->duration.'<BR />';
echo '<BR />';
}
echo '<BR />';
Example output:
Check name: Main web server
From: 2007-03-02T01:25:39-06:00
To: 2007-03-03T01:25:39-06:00
Duration: 86377
From: 2007-03-03T01:25:39-06:00
To: 2007-03-04T01:25:39-06:00
Duration: 85928
From: 2007-03-04T01:25:39-06:00
To: 2007-03-05T01:25:39-06:00
Duration: 67953
// {{{ Safe way to end connection with Pingdom API server
$logout_response = $client->Auth_logout(PINGDOM_API_KEY, $sessionId);
//Check if everything is OK
if (PINGDOM_API_STATUS_OK != $logout_response->status)
{
exit('Error occurred while closing connection');
}
// }}}
echo 'Logged out';