Skip to content

Instantly share code, notes, and snippets.

@faniska
Created January 25, 2019 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faniska/c8b33ba3c108907d9db1aedf8ea4ecf3 to your computer and use it in GitHub Desktop.
Save faniska/c8b33ba3c108907d9db1aedf8ea4ecf3 to your computer and use it in GitHub Desktop.
Site Pinger
<?php
function get_url($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "PC Tester Curl");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 600);
$content = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
return array('content'=>$content, 'httpcode'=>$httpcode, 'error'=>$error);
}
$urls[] = 'https://www.site1.com';
$urls[] = 'https://www.site2.com';
$mail_to = 'some@email.com';
foreach($urls as $url) {
sleep(10);
$response = get_url($url);
if($response['httpcode']!=200) {
sleep(2);
$response = get_url($url);
}
if($response['httpcode']!=200) {
$text = $url .' status is '.$response['httpcode'].PHP_EOL;
if(!empty($response['error'])) {
$text .= $response['error'];
}
$mail = mail($mail_to, 'Pinger Notify:'.$response['httpcode'], $text);
}
if(preg_match('#\[\[{"benchmarks":(.*?)}\]\]#', $response['content'], $match)) {
$benchmarks = json_decode($match[1], true);
$timer = (float)$benchmarks['timer'];
if($timer > 2) {
$text = $url .' status is '.$response['httpcode'].PHP_EOL;
$text .= "Timestamp: \t". date("Y-m-d H:i:s").PHP_EOL;
$text .= "Benchmarks: \t".print_r($benchmarks, true);
mail($mail_to, 'Pinger Notify: timer '.$timer, $text);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment