I am creating a website that will check if the site works and if it lives. I pass in the URL of the site I would like to check, and the following code checks if the site is alive, and returns an HTTP response code, as well as true or false.
function urlExists($url=NULL)
{
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode == 0) {
return array (false, $httpcode);
}
else if($httpcode < 400){
return array (true, $httpcode);
} else {
return array (false, $httpcode);
}
}
With one of the sites I'm testing, I get an HTTP response code of 0, although I know that the site is up and running.
The site is very slow, because its large site is on a not very powerful server, so the response time can vary from 7 to 25 seconds.
Any help would be greatly appreciated.
Thank,
Sam
source
share