We now have a youtube channel. Subscribe!

PHP - AJAX RSS Feed Tutorial


Hello folks! welcome back to a new edition of our tutorial on PHP. In this edition of our PHP tutorial, we will be studying about the AJAX RSS Feed in PHP.

What is an RSS?

RSS (Really Simple Syndication) is used in publishing often updated information from a website like audio, video, image, etc. We can integrate the RSS feeds into a website using Ajax and PHP. This code illustrates how to show RSS feeds in our website.


Index page should be as follows -

         function showRSS(str) {
            if (str.length == 0) { 
               document.getElementById("output").innerHTML = "";
            if (window.XMLHttpRequest) {
               xmlhttp = new XMLHttpRequest();
            }else {
               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            xmlhttp.onreadystatechange = function() {
               if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                  document.getElementById("output").innerHTML = xmlhttp.responseText;
      <p>Please Select an option to get RSS:</p>
         <select onchange = "showRSS(this.value)">
            <option value = "">Select an RSS-feed:</option>
            <option value = "cnn">CNN</option>
            <option value = "bbc">BBC News</option>
            <option value = "pc">PC World</option>
      <div id = "output">RSS-feeds</div>


The rss.php contains the syntax about how to get access to RSS feeds and return RSS feeds back to the web pages.

   $q = $_GET["q"];
   if($q == "cnn") {
      $xml = ("http://rss.cnn.com/rss/cnn_topstories.rss");
   }elseif($q == "bbc") {
      $xml = ("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml");
   }elseif($q = "pcw"){
      $xml = ("http://www.pcworld.com/index.rss");
   $xmlDoc = new DOMDocument();
   $channel = $xmlDoc->getElementsByTagName('channel')->item(0);
   $channel_title = $channel->getElementsByTagName('title')
   $channel_link = $channel->getElementsByTagName('link')
   $channel_desc = $channel->getElementsByTagName('description')
   echo("<p><a href = '" . $channel_link . "'>" . 
      $channel_title . "</a>");
   echo($channel_desc . "</p>");
   $x = $xmlDoc->getElementsByTagName('item');
   for ($i = 0; $i<=2; $i++) {
      $item_title = $x->item($i)->getElementsByTagName('title')
      $item_link = $x->item($i)->getElementsByTagName('link')
      $item_desc = $x->item($i)->getElementsByTagName('description')
      echo ("<p><a href = '" . $item_link . "'>" .
         $item_title . "</a>");
      echo ("<br>");
      echo ($item_desc . "</p>");


When the above code is executed, it will produce the following result -

Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about PHP - XML Introduction.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain