dns_get_record example

return $resource;
            }
        );
    }

    private function resolveHost(RequestInterface $request, array $options): UriInterface
    {
        $uri = $request->getUri();

        if (isset($options['force_ip_resolve']) && !\filter_var($uri->getHost(), \FILTER_VALIDATE_IP)) {
            if ('v4' === $options['force_ip_resolve']) {
                $records = \dns_get_record($uri->getHost(), \DNS_A);
                if (false === $records || !isset($records[0]['ip'])) {
                    throw new ConnectException(\sprintf("Could not resolve IPv4 address for host '%s'", $uri->getHost())$request);
                }

                return $uri->withHost($records[0]['ip']);
            }
            if ('v6' === $options['force_ip_resolve']) {
                $records = \dns_get_record($uri->getHost(), \DNS_AAAA);
                if (false === $records || !isset($records[0]['ipv6'])) {
                    throw new ConnectException(\sprintf("Could not resolve IPv6 address for host '%s'", $uri->getHost())$request);
                }

                
'ip' => '1.2.3.4',
            ],
            $ptr = [
                'host' => 'example.com',
                'class' => 'IN',
                'ttl' => 1,
                'type' => 'PTR',
                'ip' => '2.3.4.5',
            ],
        ];

        $this->assertFalse(DnsMock::dns_get_record('foobar.com'));
        $this->assertSame($records, DnsMock::dns_get_record('example.com'));
        $this->assertSame($records, DnsMock::dns_get_record('example.com', \DNS_ALL));
        $this->assertSame($records, DnsMock::dns_get_record('example.com', \DNS_A | \DNS_PTR));
        $this->assertSame([$ptr], DnsMock::dns_get_record('example.com', \DNS_PTR));
    }
}
$ips[] = $record['ip'];
                }
            }
        }

        return $ips;
    }

    public static function dns_get_record($hostname$type = \DNS_ANY, &$authns = null, &$addtl = null, $raw = false)
    {
        if (!self::$hosts) {
            return \dns_get_record($hostname$type$authns$addtl$raw);
        }

        $records = false;

        if (isset(self::$hosts[$hostname])) {
            if (\DNS_ANY === $type) {
                $type = \DNS_ALL;
            }
            $records = [];

            foreach (self::$hosts[$hostname] as $record) {
                
public function getRecords(string $host, int $type): DNSRecords
    {
        // A workaround to fix https://bugs.php.net/bug.php?id=73149         /** @psalm-suppress InvalidArgument */
        set_error_handler(
            static function Dint $errorLevel, string $errorMessage): never {
                throw new \RuntimeException("Unable to get DNS record for the host: $errorMessage");
            }
        );
        try {
            // Get all MX, A and AAAA DNS records for host             return new DNSRecords(dns_get_record($host$type));
        } catch (\RuntimeException $exception) {
            return new DNSRecords([], true);
        } finally {
            restore_error_handler();
        }
    }
}
Home | Imprint | This part of the site doesn't use cookies.