file_get_contents(): No such host is known error

12,207

Solution 1

Your "localhost" cannot resolve the name google.com to an ip address, which means your machine doesn't/can't reach a valid dns server.
This probably doesn't only affect php. Try ping google.com on the console of that machine. Does this complain about the unknown host, too?
The steps necessary to solve the problem depend on what your "localhost" is (operating system, flavour/distribution, ...) and how it is connected to the internet.

Solution 2

It is because you need to turn on allow_url_fopen in php.ini

Share:
12,207
Paulraj
Author by

Paulraj

Passionate Programmer who loves to code. Angular, Typescript, Javascript, Kendo-UI, PHP, Python, Mysql, MongoDB, Codeigniter, Zend, Laravel, Drupal.

Updated on June 20, 2022

Comments

  • Paulraj
    Paulraj almost 2 years

    I am using the file_get_contents function to get the URL into a string:

    $contents = file_get_contents("http://google.com");
    

    But, I am getting the following error:

    file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: No such host is known.

    How can I solve this in my localhost?

    • RaYell
      RaYell over 14 years
      I think this may be DNS or firewall issue. Can you check if you can ping google.com from that host?
    • Kamlesh
      Kamlesh almost 3 years
      ping google.com works in command line but getting same issue.
  • Paulraj
    Paulraj over 14 years
    when i give ping google.com it says unknown host. i check with my host. thanks.
  • ESP32
    ESP32 over 8 years
    That's not correct - if that was the case the server would return someting like "failed to open stream: no suitable wrapper could be found" or "URL file-access is disabled in the server configuration"
  • Kamlesh
    Kamlesh almost 3 years
    ping google.com works in command line but getting same issue
  • Kamlesh
    Kamlesh almost 3 years
    allow_url_fopen is Already ON but still not working