开发者

How to check if a list of domain names have a site?

开发者 https://www.devze.com 2023-04-01 23:46 出处:网络
I have a huge list of domain names in the form of abcde.com What I have to do is to check if the domains have a page otherwise I get the server not found message.

I have a huge list of domain names in the form of abcde.com

What I have to do is to check if the domains have a page otherwise I get the server not found message.

What is a code that will check this automatically and return开发者_高级运维 me something if there is a site ? I am familiar with PHP.

Thank you.


Something simple would be:

foreach ($domains as $domain) {
    $html =  file_get_contents('http://'.$domain);
    if ($html) {
        //do something with data
    } else {
       // page not found
    }
}

If you have them in a txt file, with each line containing the domain name you could do this:

$file_handle = fopen("mydomains.txt", "r");
    while (!feof($file_handle)) {
        $domain = fgets($file_handle);
        //use code above here
    }
}
fclose($file_handle);


You can connect to each domain/hostname using cURL.

Example:

// I'm assuming one domain per line
$h = fopen("domains.txt", "r");

while (($host = preg_replace("/[\n\r]/", "", fgets($h))) !== false) {
    $ch = curl_init($host);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if (curl_exec($ch) !== false) {
        // code for domain/host with website
    } else {
        // code for domain/host without website
    }
    curl_close($ch);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号