zeddex
zeddex

Reputation: 1300

PHP: Check if a website is online

I have a curl based function in php to check if a website is online that works fine however i have noticed that it only works for http links and not https links.

Does anyone know how this function can be updated to support https links also.

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;
    } else { 
        return false;
    }
}

Upvotes: 1

Views: 3466

Answers (3)

Matthew Way
Matthew Way

Reputation: 351

When you say, "i have noticed that it only works for http links and not https links", what exactly do you mean? cURL works the same either way, you just pass it a url with the https protocol instead of http. The problem might be something else. Does this sound like what you're having trouble with? How to send HTTPS posts using php

Upvotes: 1

Manoj Dhiman
Manoj Dhiman

Reputation: 5166

try to add this in curl function

curl_setopt($curlInit, CURLOPT_SSL_VERIFYPEER, false);

Upvotes: 1

Azeez Kallayi
Azeez Kallayi

Reputation: 2642

If you want to use your function and it u need https support then . you can download this file cacert.pem and add the path in your function like:

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);
curl_setopt($curlInit, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
curl_setopt($curlInit, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
curl_setopt ($curlInit, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
// Get answer
$response = curl_exec($curlInit);

curl_close($curlInit);

   if ($response){ 
    return 1;
   } else { 
    return 0;
   }
}
$domain = 'https://www.facebook.com';
$avi = isDomainAvailible($domain);
echo $avi;

Upvotes: 1

Related Questions