Performs a cURL-Request to check, if a website exists / is online

//Cron job comand line: /usr/local/bin/php /home/USERNAME/site-monitor/monitor.php > /dev/null
ini_set('error_log','/home/USERNAME/site-monitor/downtime.log');

//returns true, if domain is availible, false if not
function isDomainAvailible($domain) {
	//check, if a valid url is provided
	if(!filter_var($domain, FILTER_VALIDATE_URL)) {
		return false;
	}
	//initialize curl
	$curlInit = curl_init($domain);
	curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
	curl_setopt($curlInit,CURLOPT_HEADER,true);
	curl_setopt($curlInit,CURLOPT_NOBODY,true);
	curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
	//get answer
	$response = curl_exec($curlInit);
	curl_close($curlInit);
	if ($response) return true;
	return false;
}

if (!isDomainAvailible('http://www.example.com/updown.html')) {
	echo "Woops, nothing found there.";
	$message = 'Woops, Your site was down on '.date("Y-m-d H:i:s");
	$headers = 'From: [email protected]';
	mail("[email protected]", "Website is down", $message, $headers);
	error_log($message, 0);
}

Source:
http://css-tricks.com/snippets/php/check-if-website-is-available/

Pin It on Pinterest

Share This