0 follower

Final Class Yiisoft\NetworkUtilities\DnsHelper

InheritanceYiisoft\NetworkUtilities\DnsHelper

DnsHelper contains static methods to work with DNS.

Public Methods

Hide inherited methods

Method Description Defined By
acceptsEmails() Checks email's domain availability. Yiisoft\NetworkUtilities\DnsHelper
existsA() Checks DNS A record availability. Yiisoft\NetworkUtilities\DnsHelper
existsMx() Checks DNS MX record availability. Yiisoft\NetworkUtilities\DnsHelper

Method Details

Hide inherited methods

acceptsEmails() public static method

Checks email's domain availability.

public static boolean acceptsEmails ( string $hostnameOrEmail )
$hostnameOrEmail string

Hostname without dot at end or an email.

return boolean

Whether email domain is available.

                public static function acceptsEmails(string $hostnameOrEmail): bool
{
    if (strpos($hostnameOrEmail, '@') !== false) {
        /**
         * @psalm-suppress PossiblyUndefinedArrayOffset In this case `explode()` always returns an array with 2 elements.
         */
        [, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2);
    }
    return self::existsMx($hostnameOrEmail) || self::existsA($hostnameOrEmail);
}

            
existsA() public static method

Checks DNS A record availability.

public static boolean existsA ( string $hostname )
$hostname string

Hostname without dot at end.

return boolean

Whether A records exists.

                public static function existsA(string $hostname): bool
{
    set_error_handler(static function (int $errorNumber, string $errorString) use ($hostname): bool {
        throw new RuntimeException(
            sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString,
            $errorNumber
        );
    });
    /**
     * @var array $result We catch errors by `set_error_handler()` and throw exceptions if something goes wrong.
     * So `dns_get_record()` will always return an array.
     */
    $result = dns_get_record($hostname, DNS_A);
    restore_error_handler();
    return count($result) > 0;
}

            
existsMx() public static method

Checks DNS MX record availability.

public static boolean existsMx ( string $hostname )
$hostname string

Hostname without dot at end.

return boolean

Whether MX record exists.

                public static function existsMx(string $hostname): bool
{
    set_error_handler(static function (int $errorNumber, string $errorString) use ($hostname): bool {
        throw new RuntimeException(
            sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString,
            $errorNumber
        );
    });
    $hostname = rtrim($hostname, '.') . '.';
    /**
     * @var array $result We catch errors by `set_error_handler()` and throw exceptions if something goes wrong.
     * So `dns_get_record()` will always return an array.
     */
    $result = dns_get_record($hostname, DNS_MX);
    restore_error_handler();
    return count($result) > 0;
}