I have string with a comma separated list of domain names, I want to ensure that the domains are valid and the domains actually exist. Here is the Custom Validation Rule I have, but the execution of checkdnrr for MX records seems to take a lot of time and causing a 504 error. Is there a simpler way to do this?
class ValidDomainListRule implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$domains = array_map('trim', explode(',', $value));
foreach ($domains as $domain) {
// Basic domain format validation
if (!filter_var($domain, FILTER_VALIDATE_DOMAIN)) {
$fail("The {$attribute} contains an invalid domain format: {$domain}.");
return;
}
// Check if the domain exists (DNS resolution)
if (!checkdnsrr($domain, 'ANY')) { // 'ANY' checks for any type of DNS record
$fail("The domain '{$domain}' in {$attribute} does not appear to exist.");
return;
}
}
}
}
Any advice.