PHP Example - AJAX RSS Reader

An RSS Reader is utilized to peruse RSS Feeds.


The following model will exhibit a RSS peruser, where the RSS-channel is stacked into a website page without reloading:

RSS-channel will be recorded here...

Example Explained - The HTML Page

When a client chooses a RSS-channel in the dropdown list over, a capacity called "showRSS()" is executed. The work is activated by the "onchange" event:

work showRSS(str) {
  in the event that (str.length==0) {
  in the event that (window.XMLHttpRequest) {
   /code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { /code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {

<select onchange="showRSS(this.value)">
<option value="">Select a RSS-feed:</option>
<option value="Google">Google News</option>
<option value="NBC">NBC News</option>
<div id="rssOutput">RSS-channel will be recorded here...</div>

The showRSS() work does the following:

  • Check if a RSS-channel is selected
  • Create a XMLHttpRequest object
  • Create the capacity to be executed when the server reaction is ready
  • Send the demand off to a document on the server
  • Notice that a parameter (q) is added to the URL (with the substance of the dropdown list)

The PHP File

The page on the server called by the JavaScript above is a PHP document called "getrss.php":

/get the q parameter from URL

/discover which feed was selected
if($q=="Google") {
} elseif($q=="NBC") {

$xmlDoc = new DOMDocument();

/get components 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;

/yield components from "<channel>"
echo("<p><a href='" . $channel_link
  . "'>" . $channel_title . "</a>");
echo($channel_desc . "</p>");

/get and yield "<item>" elements
for ($i=0; $i<=2; $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 demand for a RSS channel is sent from the JavaScript, the accompanying happens:

  • Check which feed was selected
  • Create another XML DOM object
  • Load the RSS report in the xml variable
  • Extract and yield components from the channel element
  • Extract and yield components from the thing elements