1
<?php

header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json'); 

$jsonData = json_decode(file_get_contents(urlencode('https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json')));

echo $jsonData;

?>

Error: failed to open stream: No such file or directory in <b>C:\wamp\www\file.php

I want to print the result as a json string so i can handle it with jquery ajax. What am i missing? Thanks

Johan
  • 35,120
  • 54
  • 178
  • 293

3 Answers3

2

strip the urlencode function from your code. This formats your whole url as a string to send as a parameter in a request. You want just to pass the url to file_get_contents anyway.

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • Changed, but now i get `Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?` – Johan Mar 11 '12 at 16:04
  • 1
    @Johan check this [question: Unable to find the wrapper “https” - did you forget to enable it when you configured PHP?](http://stackoverflow.com/questions/5444249/unable-to-find-the-wrapper-https-did-you-forget-to-enable-it-when-you-config) – Sirko Mar 11 '12 at 16:08
  • Hmm, now i get `Object of class stdClass could not be converted to string in...` :) – Johan Mar 11 '12 at 16:12
  • @Johan As you only want to pass it on, you just need to echo it again. No need to de-/encode it again. – Sirko Mar 11 '12 at 16:24
2

You realise you have a space in your URL (results in a 400 from Google). Also, you don't want to use urlencode() here.

I'd also hazard a guess that you don't want to use json_decode() as this will return an object or array.

So, try this instead

readfile('https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json');
exit;

To do what you're attempting, please pay attention to this note in the manual

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

If you cannot configure your servers as such, you'll want to check out cURL or the sockets extension

Phil
  • 157,677
  • 23
  • 242
  • 245
  • My goal is to print json that i can use from a jquery ajax call. Do i still need the json_decode? – Johan Mar 11 '12 at 16:08
  • @Johan No, the Google Chart API returns JSON already, you just need to echo it (which is what `readfile()` does) – Phil Mar 11 '12 at 16:15
0
<?php

header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json'); 

$jsonData = json_decode(file_get_contents('https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json'));

echo $jsonData;

?>

This should work.

Frederick Behrends
  • 3,075
  • 23
  • 47