Warning: Php_network_getaddresses: Getaddrinfo Failed: No Such Host Is Known

Subodh Poudel Aug 09, 2022
Warning: Php_network_getaddresses: Getaddrinfo Failed: No Such Host Is Known

You might have encountered the following warning while working with databases, servers, or web services in PHP.

Warning : php_network_getaddresses: getaddrinfo failed: No such host is known

The warning is caused when your system cannot find a valid DNS server. This article will introduce a method to get rid of such warnings.

Ensure the Host Exists Using the dns_get_record() PHP Function

We can interact and read contents from a website using the file functions like fopen(), fgets() and fclose() in PHP. While opening a URL, you might encounter a warning message that says No such host is known.

The complete warning is shown at the beginning of the article. One probable reason for the warning is that the host may not exist.

Therefore, it is a good approach to check the host’s status.

We can use the dns_get_record() PHP function to see the DNS Resources Records of a particular host. We can set the hostname in the function and obtain its DNS information.

For example, create a variable $result and assign the function dns_get_record() to it. Write the hostname stackoverflow.com as the parameter to the function.

Next, use print_r() to display the information of the $result variable.

Example Code:

$result = dns_get_record("stackoverflow.com");
echo '<pre>', print_r($result), '</pre>';

Output:

Array
(
    [0] => Array
        (
            [host] => stackoverflow.com
            [class] => IN
            [ttl] => 186
            [type] => A
            [ip] => 151.101.129.69
        )

    [1] => Array
        (
            [host] => stackoverflow.com
            [class] => IN
            [ttl] => 186
            [type] => A
            [ip] => 151.101.65.69
        )

    [2] => Array
        (
            [host] => stackoverflow.com
            [class] => IN
            [ttl] => 186
            [type] => A
            [ip] => 151.101.1.69
        )

    [3] => Array
        (
            [host] => stackoverflow.com
            [class] => IN
            [ttl] => 186
            [type] => A
            [ip] => 151.101.193.69
        )

)
1

The output displays the DNS Resource Records of stackoverflow.com. It ensures the hostname exists.

If the hostname does not exist, the output will be something like this.

Output:

Array
(
)
1

You can also check the name resolution with the ping command. Type the following in the terminal.

ping stackoverflow.com

Output:

ping in terminal

If the host does not exist, the output is something like this.

ping: pokharasahar.com: Name or service not known

Here, we have used the hostname pokharasahar.com, which does not exist.

Thus, we can perform name resolution using the dns_get_record() function to ensure the domain exists. This way, we can get rid of PHP’s php_network_getaddresses: getaddrinfo failed warning.

Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn

Related Article - PHP Warning