How can one check if an external domain exists using PHP?
To check if an external domain exists using PHP, you can make a simple HTTP request to the domain and check the response code. If the response code is in the 200 range, it means the domain exists and is accessible. You can use the `get_headers()` function in PHP to retrieve the headers of the URL and then check the response code.
function checkDomainExists($url) {
$headers = @get_headers($url);
if($headers && strpos($headers[0], '200')) {
return true;
} else {
return false;
}
}
// Example usage
$url = "https://www.example.com";
if(checkDomainExists($url)) {
echo "Domain exists!";
} else {
echo "Domain does not exist.";
}