PHP Examples -AJAX RSS Reader


The RSS reader is used to read RSS feeds.


AJAX RSS Reader

In the example below, we will demonstrate an RSS reader, through which content from RSS is loaded without the webpage refreshing:


RSS-feed data list...

Example Explained - HTML Page

When a user selects an RSS-feed in the dropdown list above, a function called "showRSS()" is executed. The function is triggered by the "onchange" event:

Example

<html> <head> <meta charset="utf-8"> <title>Example Tutorial (example.com)</title> <script>function showRSS(str) { if (str.length==0) { document.getElementById("rssOutput").innerHTML=""; return; } if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari browsers execute code xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 browsers execute code xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("rssOutput").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","getrss.php?q="+str,true); xmlhttp.send(); }</script> </head> <body> <form> <select onchange="showRSS(this.value)"> <option value="">Select an RSS-feed:</option> <option value="rss">Read RSS data</option> </select> </form> <br> <div id="rssOutput">RSS-feed data list...</div> </body> </html>

The showRSS() function performs the following steps:

  • Check if an RSS-feed has been selected
  • Create an XMLHttpRequest object
  • Create a function to execute when the server response is ready
  • Send a request to the file on the server
  • Note the parameter (q) added to the end of the URL (contains the content of the dropdown list)

PHP File

Filerss_demo.xml。

The server page called via JavaScript above is a PHP file named "getrss.php":

Example

<?php //RSS file $xml="rss_demo.xml"; $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); //Read elements from "<channel>" $channel=$xmlDoc->getElementsByTagName('channel')->item(0); $channel_title = $channel->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $channel_link = $channel->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $channel_desc = $channel->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; //Output elements in "<channel>" echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>"); echo("<br>"); echo($channel_desc . "</p>"); //Output elements in "<item>" $x=$xmlDoc->getElementsByTagName('item'); for ($i=0; $i<=1; $i++) { $item_title=$x->item($i)->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>"); echo ("<br>"); echo ($item_desc . "</p>"); } ?>

When a request for an RSS feed is sent from JavaScript to the PHP file, the following occurs:

  • Check which RSS feed is selected
  • Create a new XML DOM object
  • Load the RSS document in the xml variable
  • Extract and output elements from the channel element
  • Extract and output elements from the item element
Other Extensions