Skip to content

Instantly share code, notes, and snippets.

@pingyen
Created March 7, 2018 12:55
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 pingyen/9eb8fbe3adb5645a87466fb96befc359 to your computer and use it in GitHub Desktop.
Save pingyen/9eb8fbe3adb5645a87466fb96befc359 to your computer and use it in GitHub Desktop.
URL Monitor
<?php
function getHTTPStatusCode ($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $code;
}
$expectations = array(
'https://www.facebook.com/' => 200,
'https://www.google.com/' => 302
);
$exceptions = array();
foreach ($expectations as $url => $code) {
$code2 = getHTTPStatusCode($url);
if ($code !== $code2) {
$exceptions[$url] = $code2;
}
}
if (count($exceptions) !== 0) {
$lines = array();
foreach ($exceptions as $url => $code) {
$lines[] = "$url, Expected = {$expectations[$url]}, Actual = $code";
}
mail('abc@xyz.com', 'URL Monitor Warning!', implode("\r\n", $lines));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment