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

1
  • 2
    What’s the reason for using urlencode here? Commented Mar 11, 2012 at 16:01

3 Answers 3

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

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

2 Comments

My goal is to print json that i can use from a jquery ajax call. Do i still need the json_decode?
@Johan No, the Google Chart API returns JSON already, you just need to echo it (which is what readfile() does)
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.

4 Comments

Changed, but now i get Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?
Hmm, now i get Object of class stdClass could not be converted to string in... :)
@Johan As you only want to pass it on, you just need to echo it again. No need to de-/encode it again.
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.

Comments

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.