views:

1189

answers:

5

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.mysite.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.mysite.com
The location passed into the function is http://data.mysite.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
}
A: 

please give more data
errors or code samples

w43L
This would be better as a comment on the question rather than an answer
Tom Haigh
you are right, well do that in future
w43L
A: 

Please include more information, does $contents contain anything? Remember to do json_decode($contents, true) if you want it as a php array otherwise its a stdClass that gets returned.

Could it have a problem resolving the hostname? is data.mysite.com the same machine as mysite.com?

schmilblick
A: 

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.

chaos
Localhost (LH), New Host (NH), and Data-Provider (DP). Those are my three machines involved. LH could get the data from DP, but NH cannot. Are you saying I need to check /etc/hosts of NH or DP?
Jonathan Sampson
+4  A: 

Name or service not known

DNS is broke. Can you ping data.mysite.com from a shell on the machine (assuming you have one)?

Try replacing data.mysite.com with a fixed IP address for now.

bobince
Now we're getting somewhere. Couldn't find it.
Jonathan Sampson
Can you access *any* site by name from the server? eg. `ping google.com`. If not, probably you're missing some nameserver settings in /etc/resolv.conf.
bobince
This was it. I just had to replace the URL with the IP Address. Thanks.
Jonathan Sampson
I would suggest fixing the DNS problem and not rely on hard-coding IP addresses into the code. Depending on your hosting situation, you may be bypassing some load-balancing or fault-tolerant routing configuration. Also, it will be easier to make changes in the future or even be host-independent.
Nolte Burke
A: 

Also you can try curl:

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'http://url.url');
$result = curl_exec($curl);
curl_close($curl);

And you get what you want in $result.

Rytis