Skip to content

Instantly share code, notes, and snippets.

@iwek
Last active December 3, 2022 14:03
Show Gist options
  • Save iwek/5117769 to your computer and use it in GitHub Desktop.
Save iwek/5117769 to your computer and use it in GitHub Desktop.
PHP Script for Traceroute Pingdom API
<?php
$host = $_GET["host"];
if (empty($host)) {
echo 'Please provide host';
} else {
//create the url for API call
$url = "https://api.pingdom.com/api/2.0/traceroute?host=".$host;
//initialize
$ch = curl_init();
// 2. set the options, including the url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "EMAIL:PASSWORD");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("App-Key: YOUR-KEY-HERE"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute
$output = curl_exec($ch);
if ($output === FALSE) {
echo "cURL Error: " . curl_error($ch);
} else {
$response = json_decode($output,true);
$string = $response['traceroute']['result'];
//pattern to find ip addresses from string
$pattern = "/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/";
//run regex and return matches in an array
preg_match_all($pattern, $string, $ips);
//convert array to a comma seperated string of ip addresses
echo implode(",", $ips[0]);
}
//free up the curl handle
curl_close($ch);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment