Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : file_get_contents($loc) fails

Tags:

php

I just moved a project from localhost over to my remote server, and noticed that some of my scripts stopped working. Most importantly was one that relied upon file_get_contents() to fetch JSON values from another script.

PHP Version is 5.2.4
allow_url_fopen is ON

Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/html/2009/functions/functions.products.php on line 5

Warning: file_get_contents(http://data.example.com/new-data.php) [function.file-get-contents]: failed to open stream: Success in /var/www/html/2009/functions/functions.products.php on line 5

The script is being ran from: http://www.example.com
The location passed into the function is http://data.example.com/new-data.php

Note: Same domain name, but two different servers.

function getData() {
  $location = "http://data.mysite.com/new-data.php";
  $contents = file_get_contents($location);
  $jsonVars = json_decode($contents);
  return $jsonVars
}
like image 918
Sampson Avatar asked Feb 12 '09 16:02

Sampson


1 Answers

Look at your /etc/hosts on the remote server. If it's empty, you need to add '127.0.0.1 localhost' to it.

Unless it's one of the varieties of VPS where the loopback interface hits the outer machine; on those, you need to use your VPS's IP number instead of 127.0.0.1.

like image 196
chaos Avatar answered Sep 28 '22 09:09

chaos