Reputation: 35194
<?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
Upvotes: 1
Views: 848
Reputation: 164767
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
Upvotes: 2
Reputation: 3095
<?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.
Upvotes: 0
Reputation: 74046
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.
Upvotes: 2