So I'm tryign to check if a site is up or down with curl. So here's my logic
- Strip URL of protocol and anything else, leave just host.
example.com
- Add
http
protocol to URL, then follow redirect with curl
So here's some code
if(isset($_GET['url'])){
$domain = $_GET['url'];
//Clean up domain
$input = trim($domain, '/');
// If scheme not included, prepend it
if (!preg_match('#^http(s)?://#', $input)) {
$input = 'http://' . $input;
}
$urlParts = parse_url($input);
// remove www
$domain = preg_replace('/^www\./', '', $urlParts['host']);
function addhttp($domain) {
if (!preg_match("~^(?:f|ht)tps?://~i", $domain)) {
$domain = "http://" . $domain;
}
return $domain;
}
$domain = addhttp($domain);
date_default_timezone_set('America/New_York');
$date = date('m/d/Y h:i:s a', time());
}
And my function to check if the site is up or down
function isDomainAvailible($domain){
if(!filter_var($domain, FILTER_VALIDATE_URL)){
return false;
}
$curlInit = curl_init($domain);
curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curlInit,CURLOPT_HEADER,true);
curl_setopt($curlInit,CURLOPT_NOBODY,true);
curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curlInit, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($curlInit);
curl_close($curlInit);
if($response)return true;
return false;
}
Now the issue is, when I do a website that has SSL, therefore having a redirect, I always seem to get that the site is up, even though it's down. Any ideas? Is my logic in the right direction?