WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Example - AJAX RSS Reader


An RSS Reader is utilized to peruse RSS Feeds.


AJAX RSS Reader

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:

<html>
<head>
<script>
work showRSS(str) {
  in the event that (str.length==0) {
    document.getElementById("rssOutput").innerHTML="";
    return;
  }
  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) {
      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 a RSS-feed:</option>
<option value="Google">Google News</option>
<option value="NBC">NBC News</option>
</select>
</form>
<br>
<div id="rssOutput">RSS-channel will be recorded here...</div>
</body>
</html>

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":

<?php
/get the q parameter from URL
$q=$_GET["q"];

/discover which feed was selected
if($q=="Google") {
  $xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
} elseif($q=="NBC") {
  $xml=("http://rss.msnbc.msn.com/id/3032091/gadget/rss/rss.xml");
}

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

/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("<br>");
echo($channel_desc . "</p>");

/get and yield "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
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