Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP cannot turn a hostname into an IP address

ErrorWarning: fsockopen(): php_network_getaddresses: getaddrinfo failed

Short 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.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04, Docker Engine 27
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.conf
  2. 2Match what you see, then jump to its fix

    1. 1getent prints nothing, and resolv.conf has no nameserver line or lists servers that do not answerThe server has no working DNS resolverFix
    2. 2The same code works on the host but fails inside the Docker containerThe container's DNS does not reach the outsideFix
    3. 3Only 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

What does the error look like?

  • Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed
  • Warning: 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.com

2Fix

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:2

3Verify

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

Did this fix it?

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.com

2Fix

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-image

3Verify

Resolve the name again from inside the container.

docker exec my-php getent hosts example.com

Confirmed by docs.docker.com

Did this fix it?

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

Did this fix it?

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:

Related fixes

Back 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.