2

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`
3
  • Server/DNS configuration problem perhaps? Commented Apr 23, 2013 at 3:35
  • 2
    make sure allow_url_fopen = 1 in php.ini Commented Apr 23, 2013 at 3:36
  • and also echo $data; is not right. you need to do print_r($data). print_r is for array. Commented Apr 23, 2013 at 3:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

the var_dump($data); worked for this. and the server problems were from running it on a code testing site. i ran it on my actual server and everything worked fine.
Please don't forget to mark as correct if the post helped you. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.