PHP Instance-AJAX RSS Reader
RSS reader is used to read RSS feeds.
AJAX RSS Reader
In the following example, we will show an RSS reader, through which the content from RSS is loaded in the event that the page is not refreshed:
Rss-feed Data List ... Example explanation-HTML page
When the user selects a rss-feed in the drop-down list above, a function named "Showrss ()" is executed. This function is triggered by the "onchange" event:
<HTML><Head><MetaCharSet= "Utf-8"><title>Beginner's Tutorial (runoob.com)</title><Script>functionShowrss (str) {if(Str.length==0) {document.getElementById ("Rssoutput"). InnerHTML=""; return; } if(window. XMLHttpRequest) {//ie7+, Firefox, Chrome, Opera, Safari browser code executionXMLHTTP=NewXMLHttpRequest (); } Else { //IE6, IE5 browser execution codeXMLHTTP=NewActiveXObject ("Microsoft.XMLHTTP"); } Xmlhttp.onreadystatechange=function() { if(Xmlhttp.readystate==4 &&Xmlhttp.status== $) {document.getElementById ("Rssoutput"). InnerHTML=Xmlhttp.responsetext; }} xmlhttp.open ("GET","getrss.php?q="+str,true); Xmlhttp.send ();}</Script></Head><Body><form><Selectonchange= "Showrss (this.value)"><optionvalue="">Select a rss-feed:</option><optionvalue= "RSS">Reading RSS Data</option></Select></form><BR><DivID= "Rssoutput">Rss-feed Data List ...</Div></Body></HTML>
The Showrss () function performs the following steps:
- Check if any rss-feed are selected
- Create a XMLHttpRequest Object
- Create a function that executes when the server responds ready
- Send a request to a file on the server
- Note the parameter (q) added to the end of the URL (contains the contents of the drop-down list)
PHP file
File Rss_demo.xml.
The above server page that is called by JavaScript is a php file named "getrss.php":
<?PHP//RSS Files$xml= "Rss_demo.xml";$xmlDoc=NewDOMDocument ();$xmlDoc->load ($xml);//reading 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 The elements in the <channel>Echo("<p><a href= '".$channel _link. ">".$channel _title. "</a>");Echo("<br>");Echo($channel _desc. "</p>");//output The elements in the <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 a PHP file, it occurs:
- Check which RSS feed is selected
- To create a new XML DOM object
- Loading an RSS document in an XML variable
- Extracting and outputting elements from the channel element
- Extracting and outputting elements from the item element
PHP Instance-AJAX RSS Reader