{"id":5164,"date":"2013-11-13T05:02:48","date_gmt":"2013-11-12T18:02:48","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=5164"},"modified":"2013-11-13T05:02:48","modified_gmt":"2013-11-12T18:02:48","slug":"php-host-your-own-public-feed-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-host-your-own-public-feed-tutorial\/","title":{"rendered":"PHP Host Your Own Public Feed Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/feed.php\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"PHP Host Your Own Public Feed Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/Feed_JSON_Primer.jpg\" title=\"PHP Host Your Own Public Feed Tutorial\" \/><\/a><p class=\"wp-caption-text\">PHP Host Your Own Public Feed Tutorial<\/p><\/div>\n<p>Here is a tutorial that introduces you to the idea that you can not only <b>USE<\/b> public data sources of JSON data, for instance, to construct informational web applications, but, if you host a website you could also <b>HOST<\/b> a (JSON) data feed, to share with other Internet users (some data of interest).<\/p>\n<p>Lately we have been referring a lot to two big PHP functions of use for the  <b>USE<\/b> scenario (as mentioned above):<\/p>\n<ul>\n<li><a target=_blank title='file_get_contents' href='http:\/\/us1.php.net\/file_get_contents'>file_get_contents<\/a><\/li>\n<li><a target=_blank title='json_decode' href='http:\/\/php.net\/manual\/en\/function.json-decode.php'>json_decode<\/a><\/li>\n<\/ul>\n<p>So for the  <b>HOST<\/b> scenario you may have guessed it would involve:<\/p>\n<ul>\n<li><a target=_blank title='json_encode' href='http:\/\/php.net\/manual\/en\/function.json-encode.php'>json_encode<\/a><\/li>\n<li><a target=_blank title='file_put_contents' href='http:\/\/us1.php.net\/file_put_contents'><strike>file_set_contents<\/strike>&nbsp;file_put_contents<\/a><\/li>\n<\/ul>\n<p>Ah, the cycle of life &#8230; or is it how humans organize it?!   Probably, mathematicians would have an opinion on this.<\/p>\n<p>Looking at PHP&#8217;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).<\/p>\n<p><script> function showarr() {   alert('$months = array(\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\", \"September\", \"October\", \"November\", \"December\");');  } <\/script><\/p>\n<p>So for today&#8217;s <a target=_blank title='click picture' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/feed.php\">tutorial<\/a> we set up a simple feed of an <a target='#' onmouseover='showarr();'><u>array of month names<\/u><\/a> (mainly via &#8230;<\/p>\n<blockquote><p>file_put_contents(&#8220;minifeed.html&#8221;, json_encode($months));<\/p><\/blockquote>\n<p>) &#8230; 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 &#8230; cute, huh?) via &#8230;<\/p>\n<blockquote><p>file_put_contents(&#8220;minifeed.php&#8221;, &#8220;&lt;?php $pageContents = file_get_contents(&#8216;minifeed.html&#8217;); $json = json_decode($pageContents); if (isset($_GET[&#8216;feed&#8217;])) header(&#8216;Location: minifeed.html&#8217;); else var_dump($json); ?&gt;&#8221;);<\/p><\/blockquote>\n<p> &#8230; and then read the feed and var_dump (as you can see from the code above, it would do) when you &#8230;<\/p>\n<blockquote><p>header(&#8220;Location: minifeed.php&#8221;);<\/p><\/blockquote>\n<p> &#8230; the upshot of which is that <a target=_blank title='http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/minifeed.php?feed=y' href='http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/minifeed.php?feed=y'>http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/minifeed.php?feed=y<\/a> (or just  <a target=_blank title='http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/minifeed.html' href='http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/minifeed.html'>http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/minifeed.html<\/a>) become URLs to share with Internet users as a Public Feed opportunity.<\/p>\n<p>Another tool you should have in your armoury for jobs like this is the online JSON validator <a target=_blank title='JSON validator online' href='http:\/\/jsonlint.com\/'>here<\/a>. <\/p>\n<p>Here is a link to some downloadable PHP programming source code (for the tutorial) which you may want to rename to <a target=_blank title='feed.php' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/feed.php_GETME\">feed.php<\/a><\/p>\n<p>Here is a link to some downloadable PHP programming source code (for the public feed) which you may want to rename to <a target=_blank title='minifeed.php' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/minifeed.php_GETME\">minifeed.php<\/a> (?feed=y)<\/p>\n<p>Here is another link to some programming source code (for the public feed) which you may want to rename to <a target=_blank title='minifeed.html' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/JSON\/minifeed.html_GETME\">minifeed.html<\/a><\/p>\n<p>One last tool of interest is the PHP command <a target=_blank title='var_dump' href='http:\/\/php.net\/manual\/en\/function.var-dump.php'>var_dump<\/a> as used in this tutorial, which can be useful to display the structure of a PHP variable (usually with some complexity).<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d5164' onclick='var dv=document.getElementById(\"d5164\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=JSON\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d5164' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-host-your-own-public-feed-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,12,33,37],"tags":[412,664,932,997,1166,1319],"class_list":["post-5164","post","type-post","status-publish","format-standard","hentry","category-data-integration","category-elearning","category-software","category-tutorials","tag-feed","tag-json","tag-php","tag-programming","tag-software-2","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/5164"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=5164"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/5164\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=5164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=5164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=5164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}