user2193004
user2193004

Reputation: 89

php code (getting json results)

i am completely new to php from python. in python, i am fairly familiar with getting json results from webpages. however whatever i seem to do in php is not working. the code i am using is.

 <?php 
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello&safe=off";
$data = json_decode(file_get_contents($url));
echo $data; 
?>`

yet this does absolutely nothing. is there anything wrong with what i am doing? also i ran it through ideone.com and got these errors.

`PHP Warning:    file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/VQMKwy/prog.php on line 3

PHP Warning:file_get_contents(http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello&safe=off): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/VQMKwy/prog.php on line 3`

Upvotes: 2

Views: 271

Answers (1)

Saturnix
Saturnix

Reputation: 10564

As soon as you'll solve the problem posted above (which seems related to your server configuration, as file_get_contents works fine for me) you'll encounter another error.

json_decode will return an object of class stdClass. You can't just echo that out. The fastest way to echo an array is to use

var_dump($data);

apart from that, the code runs fine on my local machine. I've just replaced echo with var_dump


The hint posted by Rob should solve the problem but if it doesn't and if you're running on Apache try to run this command:

sudo rc.d stop httpd; sudo rc.d start httpd

this will force Apache to stop and start thus updating /etc/resolv.conf

Upvotes: 1

Related Questions