PHP Example - AJAX RSS Reader

« Previous Chapter Next Chapter »

An RSS Reader is used to read RSS Feeds.


The Below example will demonstrate an RSS reader, where the RSS-feed is loaded into a webpage without reloading:

RSS-feed will be listed here...

Example Explained - The HTML Page

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

<script type="text/javascript">
function showRSS(str)
if (str.length==0)
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  if (xmlhttp.readyState==4 && xmlhttp.status==200)

<select onchange="showRSS(this.value)">
<option value="">Select an RSS-feed:</option>
<option value="Google">Google News</option>
<option value="MSNBC">MSNBC News</option>
<br />
<div id="rssOutput">RSS-feed will be listed here...</div>

The showResult() function does the following:

  • Check if an RSS-feed is selected
  • Create an XMLHttpRequest object
  • Create the function to be executed when the server response is ready
  • Send the request off to a file on the server
  • Note: a parameter (q) is added to the URL (with Content of the dropdown list)

The PHP File

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

//get the q parameter from URL

//find out which feed was selected

$xmlDoc = new DOMDocument();

//get elements from "<channel>"
$channel_title = $channel->getElementsByTagName('title')
$channel_link = $channel->getElementsByTagName('link')
$channel_desc = $channel->getElementsByTagName('description')

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

//get and output "<item>" elements
for ($i=0; $i<=2; $i++)

  echo ("<p><a href='" . $item_link
  . "'>" . $item_title . "</a>");
  echo ("<br />");
  echo ($item_desc . "</p>");

When an RSS-feed is sent from the JavaScript, the following happens:

  • Check which feed was 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

« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!