file_get_contents fails with "getaddrinfo failed: no address associated with hostname"

16,529

It seems that allow_url_fopen is disabled on you php.ini, that's why file_get_contents isn't working.
Try using curl instead:

$url = "http://www.example.com/";
$ch = curl_init();  
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$page = curl_exec($ch);
curl_close($ch);
echo $page;
Share:
16,529
ba3a
Author by

ba3a

Java developer, math scientist. Former PhD student. Guitarplayer. I live in Moscow. I'm russian. English isn't my mother tongue.

Updated on June 23, 2022

Comments

  • ba3a
    ba3a almost 2 years

    I'm trying to get the page from another host. I do as hph manual says:

        $page = file_get_contents('http://www.example.com/');
        echo $page;
    

    But it fails and in apache log i get the following:

    [Mon Oct 12 18:58:47.676454 2015] [:error] [pid 2971] [client 127.0.0.1:49434] PHP Warning:  file_get_contents(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /var/www/html/digest/ftry.php on line 2
    [Mon Oct 12 18:58:47.704659 2015] [:error] [pid 2971] [client 127.0.0.1:49434] PHP Warning:  file_get_contents(http://www.example.com/): failed to open stream: php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /var/www/html/digest/ftry.php on line 2
    

    While trying to figure out the reason of failure, I checked what nslookup says. When I run nslookup http://www.example.com/ it answers something like that:

    nslookup http://www.example.com/
    Server:         94.242.57.130
    Address:        94.242.57.130#53
    
    ** server can't find http://www.example.com/: NXDOMAIN
    

    When I remove http:// from domain name it works fine, returns ip. But when I try to get file with file_get_contents without http://, it looks for the file in the local filesystem (and it's how it's supposed to be).

    I've checked phpinfo, allow_url_fopen is On on both local and master levels. I've set the nameserver in my resolv.conf to 94.242.57.130 (nearest public dns).