Resolving a hostname to its current IP addresses matters when PHP code needs to connect to an upstream service, verify where a domain currently points, or record the network endpoints behind a name before opening a socket.
In PHP, dns_get_record() is the practical lookup path when both A and AAAA answers need to stay available. Requesting only those record types returns structured arrays with the record type, TTL, and address field already separated, which keeps IPv4 and IPv6 handling clearer than the older shortcut helpers.
Returned data still depends on the resolver available to the host running PHP. Search domains, local hosts-file overrides, and DNS rotation can change the answer set between systems or between runs, so fully qualified hostnames and explicit empty-result handling keep the result predictable. Current PHP docs also keep gethostbyname() and gethostbynamel() in the IPv4-only category, with gethostbyname() returning the original hostname string when resolution fails. The examples below use a masked fully qualified hostname with documentation-range IP addresses so the output shape stays realistic without exposing a live endpoint.
Related: Get a hostname from a URL using PHP
Steps to resolve a hostname to IP addresses in PHP:
- Create a small PHP helper that requests only A and AAAA records and normalizes them into one list.
- hostname-to-ip.php
<?php function resolveHostnameToAddresses(string $hostname): array { $records = dns_get_record($hostname, DNS_A | DNS_AAAA); if ($records === false) { throw new RuntimeException("DNS query failed for {$hostname}"); } $addresses = []; foreach ($records as $record) { $type = $record['type'] ?? null; if ($type === 'A' && isset($record['ip'])) { $addresses[] = [ 'type' => 'A', 'address' => $record['ip'], ]; continue; } if ($type === 'AAAA' && isset($record['ipv6'])) { $addresses[] = [ 'type' => 'AAAA', 'address' => $record['ipv6'], ]; } } if ($addresses === []) { throw new RuntimeException("No A or AAAA records found for {$hostname}"); } return $addresses; } $hostname = trim($argv[1] ?? ''); if ($hostname === '') { fwrite(STDERR, "Usage: {$argv[0]} <hostname>" . PHP_EOL); exit(1); } try { $addresses = resolveHostnameToAddresses($hostname); } catch (RuntimeException $exception) { fwrite(STDERR, $exception->getMessage() . PHP_EOL); exit(1); } foreach ($addresses as $entry) { echo $entry['type'], ': ', $entry['address'], PHP_EOL; }
dns_get_record() keeps the record type and address family visible in one response. gethostbyname() returns one IPv4 string, and gethostbynamel() returns an IPv4 list.
- Run the script with a fully qualified hostname and confirm that each returned address is labeled by record type.
$ php hostname-to-ip.php edge-api01.prod.example.net A: 198.51.100.24 A: 198.51.100.25 AAAA: 2001:db8:100::24 AAAA: 2001:db8:100::25
The exact address list and its order can change between runs or between resolvers. Use a fully qualified hostname instead of a short local name when search domains could rewrite the query.
- Run the script with a reserved invalid hostname and confirm that missing A or AAAA answers stop the workflow explicitly.
$ php hostname-to-ip.php edge-api01.invalid No A or AAAA records found for edge-api01.invalid
Current PHP builds commonly return an empty array for an unresolved hostname or a name that has no matching A or AAAA answer. Treat the false branch as a genuine DNS query failure instead of a normal no-result case.
- Apply an explicit address-selection rule before passing one resolved value into a socket or HTTP client.
$addresses = resolveHostnameToAddresses($hostname); $preferred = null; foreach ($addresses as $entry) { if ($entry['type'] === 'AAAA') { $preferred = $entry['address']; break; } } if ($preferred === null) { $preferred = $addresses[0]['address']; } echo $preferred, PHP_EOL;
Prefer an explicit selection rule such as AAAA first, A first, or round-robin. DNS answer order is not a stable contract for application behavior.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
