1

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;
    }
}
zeddex
  • 1,260
  • 5
  • 21
  • 38
  • 1
    alternatively you can try this for domain checking http://www.phpwhois.org/ – Manoj Dhiman Mar 23 '15 at 12:30
  • 1
    https://github.com/HelgeSverre/Domain-Availability – Manoj Dhiman Mar 23 '15 at 12:37
  • Thanks for the links, i didn't want availability however but rather current online/offline status for the website. My function works fine but only for http and not https. I have a feeling there needs to be some extra curl added. – zeddex Mar 23 '15 at 12:45

3 Answers3

1

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;
Azeez Kallayi
  • 2,567
  • 1
  • 15
  • 19
  • Thanks this works great, 2 quick questions. 1) Is there a page with more info on the certificates thing (pem) and 2) I know it's valid but why do you use 1 and 0 rather than true and false booleans in your code is there any specific reason? – zeddex Mar 23 '15 at 12:54
  • http://serverfault.com/questions/241046/what-is-the-cacert-pem-and-for-what-to-use-that – zeddex Mar 23 '15 at 13:14
  • You are welcome . No specific reason for 1 and 0 . just put for testing :) – Azeez Kallayi Mar 23 '15 at 13:20
1

try to add this in curl function

curl_setopt($curlInit, CURLOPT_SSL_VERIFYPEER, false);
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
1

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

Community
  • 1
  • 1
Matthew Way
  • 331
  • 1
  • 4
  • I mean if you pass a https link it returns as false for some reason. I have just accepted an answer that uses certificates which fixes this issue. – zeddex Mar 23 '15 at 12:56
  • Ah i see you add the extra item and it works. I just upvoted your answer. – zeddex Mar 23 '15 at 13:02