Simon H
Simon H

Reputation: 374

Why does curl say all websites with http are available?

I am using a cURL script to check if a website is available.

It does not work properly though; according to it, all URL's that have http:// are available. Why does this do this?

Here is my code:

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;
    }

Thank you!

Upvotes: 2

Views: 142

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173652

Judging from the code, it seems that you're either behind a transparent web proxy or being served by a non-standard name server;

A transparent proxy will make a request on your behalf and return its response. If that's the case, it seems that in the case of a non-existent location it will still return a 200 status code.

A non-standard name server may return the IP address of a web server that hosts a custom landing page (think of "domain for sale") instead of returning a lookup error.

You can request cURL to provide more debugging information by setting the appropriate option:

curl_setopt($ch, CURLOPT_VERBOSE, 1);

Lastly, check the returned body to see what is being returned. It should be obvious from there which of the above scenarios applies here.

Upvotes: 1

Rakesh Sharma
Rakesh Sharma

Reputation: 13738

try with curl_getinfo() for getting curl exec info

//get answer
$response = curl_exec($curlInit);
echo '<pre>';
print_r($response);
print_r(curl_getinfo($curlInit));
$info = curl_getinfo($curlInit);
if ($info['http_code'] == '200')
  return true;
else 
  retrun 'Curl error: ' . curl_error($curlInit);

also use ssl verifypeer some of sites using ssl

curl_setopt($curlInit, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlInit, CURLOPT_SSL_VERIFYPEER, 0);

Also domain must be like :- http://www.example.com (with http://) If you specify URL without protocol:// prefix, curl will attempt to guess what protocol you might want. It will then default to HTTP but try other protocols based on often-used host name prefixes. For example, for host names starting with "ftp." curl will assume you want to speak FTP. read :- http://curl.haxx.se/docs/manpage.html

for more info read the manual :- http://www.php.net/manual/en/book.curl.php

Upvotes: 0

Related Questions