Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP AJAX RSS Reader Example

PHP Example - AJAX RSS Reader


An 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 into the web page without it being refreshed:

RSS-feed data list...


Example Explanation - HTML Page

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

Example

<html>
<head>
<meta charset="utf-8">
<title>

Runoob Tutorial(runoob.com)

title

<

script

function showRSS(str) { if (str.length==0) { document.getElementById("rssOutput").innerHTML=""; return; } if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari browser execution code xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 browser execution 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:


PHP File

file rss_demo.xml.

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

Example

load ( $xml ) ; // from " " read element $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 " " element echo ( "

" . $channel_title . " " ) ; echo ( "
" ) ; echo ( $channel_desc . "

" ) ; // output " " element $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 ( "

" . $item_title . " " ) ; echo ( "
" ) ; echo ( $item_desc . "

" ) ; } ?>

When the RSS feed request is sent from JavaScript to the PHP file, the following happens:

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