Here is a tutorial that introduces you to the idea that you can not only USE public data sources of JSON data, for instance, to construct informational web applications, but, if you host a website you could also HOST a (JSON) data feed, to share with other Internet users (some data of interest).
Lately we have been referring a lot to two big PHP functions of use for the USE scenario (as mentioned above):
So for the HOST scenario you may have guessed it would involve:
Ah, the cycle of life … or is it how humans organize it?! Probably, mathematicians would have an opinion on this.
Looking at PHP’s file_put_contents is a lot like those really useful VB.Net functions System.IO.File.AppendAllText and System.IO.File.WriteAllText (all of which save a lot of coding time).
So for today’s tutorial we set up a simple feed of an array of month names (mainly via …
file_put_contents(“minifeed.html”, json_encode($months));
) … and then (bear in mind the rest of the feed.php code is just there to prove the client can use the data feed you have created) create a feed PHP file on the fly called minifeed.php (PHP creates PHP … cute, huh?) via …
file_put_contents(“minifeed.php”, “<?php $pageContents = file_get_contents(‘minifeed.html’); $json = json_decode($pageContents); if (isset($_GET[‘feed’])) header(‘Location: minifeed.html’); else var_dump($json); ?>”);
… and then read the feed and var_dump (as you can see from the code above, it would do) when you …
header(“Location: minifeed.php”);
… the upshot of which is that http://www.rjmprogramming.com.au/PHP/JSON/minifeed.php?feed=y (or just http://www.rjmprogramming.com.au/PHP/JSON/minifeed.html) become URLs to share with Internet users as a Public Feed opportunity.
Another tool you should have in your armoury for jobs like this is the online JSON validator here.
Here is a link to some downloadable PHP programming source code (for the tutorial) which you may want to rename to feed.php
Here is a link to some downloadable PHP programming source code (for the public feed) which you may want to rename to minifeed.php (?feed=y)
Here is another link to some programming source code (for the public feed) which you may want to rename to minifeed.html
One last tool of interest is the PHP command var_dump as used in this tutorial, which can be useful to display the structure of a PHP variable (usually with some complexity).
If this was interesting you may be interested in this too.