When I use following URL in browser then it prompt me to download a text file with JSOn content.
https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json
(Click above URL see downloaded file content)
Now I want to create a php page. I want that when I call this php page, it should call above URL and get content(json format) from file and show it on screen.
How can I do this ??
Use
file_get_contents
in combination withjson_decode
andecho
.Now you can either echo the $json variable, if you just want to display the output, or you can decode it, and do something with it, like so:
1) local simplest methods
2) Better Way is CURL:
It automatically handles FOLLOWLOCATION problem + Remote urls:
src="./imageblabla.png"
turned into:src="http://example.com/path/imageblabla.png"
Code : https://github.com/tazotodua/useful-php-scripts/blob/master/get-remote-url-content-data.php
Depending on your PHP configuration, this may be a easy as using:
However, if
allow_url_fopen
isn't enabled on your system, you could read the data via CURL as follows:Incidentally, if you just want the raw JSON data, then simply remove the
json_decode
.Don't forget: to get HTTPS contents, your OPENSSL extension should be enabled in your php.ini. (how to get contents of site use HTTPS)