PHP cannot turn a hostname into an IP address
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failedShort answer
This warning means the operating system could not resolve the hostname PHP was asked to connect to. PHP is not the broken part. Test the name with getent from the same machine or container, then fix the resolver list in /etc/resolv.conf, the Docker DNS setup, or the host string you pass to the function.
Fix it fast
1Find out which cause you have
Resolve the same hostname with the system resolver, from the same place PHP runs (the host, the PHP-FPM server or inside the container). If this fails too, PHP is innocent.
getent hosts example.com cat /etc/resolv.conf2Match what you see, then jump to its fix
- 1
getent prints nothing, and resolv.conf has no nameserver line or lists servers that do not answerThe server has no working DNS resolverFix - 2
The same code works on the host but fails inside the Docker containerThe container's DNS does not reach the outsideFix - 3
Only one call fails, and the name in the message looks odd (a full URL, a space, a trailing character)The host string is not a plain hostnameFix
- 1
What does the error look like?
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failedWarning: file_get_contents(): php_network_getaddresses: getaddrinfo failed
Cause 1 of 3
The server has no working DNS resolver
1Check
The resolver library reads /etc/resolv.conf. With no nameserver line it asks only the local machine, and with unreachable servers it waits for the timeout and retries before giving up.
grep -E '^(nameserver|options)' /etc/resolv.conf
getent hosts example.com2Fix
Point the machine at a resolver that answers (your provider's or one your network team runs). On a system that manages the file for you, change the setting in that tool rather than editing the generated file.
# /etc/resolv.conf (only if the file is not generated for you)
nameserver 192.0.2.53
options timeout:2 attempts:23Verify
The name resolves from the shell, then the PHP call works.
getent hosts example.com
php -r 'var_dump(gethostbyname("example.com"));'Confirmed by man7.org
Cause 2 of 3
The container's DNS does not reach the outside
1Check
A container on the default bridge gets a copy of the host's resolv.conf. A container on a custom network uses Docker's embedded DNS at 127.0.0.11, which passes outside lookups on to the host's servers. If the host's servers are loopback-only or blocked, both break.
docker exec my-php cat /etc/resolv.conf
docker exec my-php getent hosts example.com2Fix
Give the container explicit DNS servers with --dns (repeat the flag for more than one). Do not use 127.0.0.1 there: inside the container it means the container itself.
docker run --dns 192.0.2.53 --dns 192.0.2.54 my-php-image3Verify
Resolve the name again from inside the container.
docker exec my-php getent hosts example.comConfirmed by docs.docker.com
Cause 3 of 3
The host string is not a plain hostname
1Check
fsockopen() wants a hostname, optionally with a transport prefix such as ssl:// or tls://, and the port as a separate argument. A value such as https://example.com/path or a name read from a config line with stray whitespace is not a valid domain, and the manual says an invalid domain raises a warning.
var_dump($host); // look for scheme, path, spaces
var_dump(parse_url($url, PHP_URL_HOST));2Fix
Extract the host with parse_url(), trim it, and pass the port on its own.
$host = trim(parse_url($url, PHP_URL_HOST));
$fp = fsockopen('tls://' . $host, 443, $errno, $errstr, 10);3Verify
The call returns a stream, not false.
var_dump($fp, $errno, $errstr);Confirmed by php.net
How do I stop it coming back?
- Keep the hostname in configuration as a bare name, and build URLs from it in code.
- Log $errno and $errstr from fsockopen() so the real resolver message reaches your logs.
- Give containers explicit DNS servers instead of relying on whatever the host had that day.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Back end
PHP says curl_init() does not exist because an extension is missing
Fatal error: Call to undefined function mb_strlen()
The function belongs to an extension (curl, mbstring, xml and others) that the PHP process running your code has not loaded.
4 causes, each with a fixBack end
imagecreatetruecolor() is undefined because the GD extension is missing
Fatal error: Uncaught Error: Call to undefined function imagecreatetruecolor()
All imagecreate* and imagecreatetruecolor functions come from the GD extension.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
CodeIgniter 4 cannot write its cache files
CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/
CodeIgniter 4's file cache needs a cache directory that the web server can really write to.
2 causes, each with a fix