Skip to content

Instantly share code, notes, and snippets.

@brunomonteiro3
Created February 14, 2017 23:50
Show Gist options
  • Save brunomonteiro3/acca570664660569d6c59d7699411aa6 to your computer and use it in GitHub Desktop.
Save brunomonteiro3/acca570664660569d6c59d7699411aa6 to your computer and use it in GitHub Desktop.
PHP - Page status checker. Based on a cURL request, this script will check if an element is rendered on the requested URL and if it's not found, fire an e-mail alert.
<?php
/*
Author: Monteiro, Bruno
E-mail: brunomonteiro3@gmail.com
*/
// Simple cURL settings to retrive all data from the submitted URL
function page_status_checker( $url ) {
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
// Store submitted URL in a variable
$check_url = $_GET['url'];
// Store submitted URL data array in a variable
$result = page_status_checker($check_url);
// Only the content is relevant, so this will be stored in a variable
$content = $result['content'];
// Checking if the HTML comment is available on the rendered content from the submitted URL
if (strpos($content, '<!-- Rendering OK -->') == false) :
// Visual debug - Probably will be removed
echo "Something is wrong with " . $check_url . "<br />";
echo "Submitting alert e-mail...";
// Fire an e-mail alert
$to = 'email@domain.com';
$subject = 'Alert!' . $check_url . ' is down!';
$message = 'Something is wrong with' . $check_url;
$headers = 'From: email@domain.com' . "\r\n" .
'Reply-To: email@domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment