RSS Feed Reader using Ajax and PHP Part 1

Written By: Jay

- 26 Jul 2006 -
















Description: This HOWTO will explain how to build an RSS Feed Reader using AjaxAgent and PHP that can read the latest posts from multiple Wordpress Blogs.

Introduction

In this HOWTO I will explain how to build an RSS Feed Reader using AjaxAgent and PHP that can read the latest posts from multiple Wordpress Blogs.

I have my own domain name of gotfoo.org and since I tend to write about many subjects I felt that it was important to use sub-domains to seperate the content. I did this in the style of slashdot.org so I have linux.gotfoo.org and you.gotfoo.org. This setup works fine but I still want to just give out the root domain gotfoo.org for any shameless plugs and what not.

For my "homepage" I wanted to have a the latest posts with a link directly to the content but I did not want to have to hack Wordpress and make a database call or anything. Instead I wanted to hook into the existing RSS feed that is provided by the WP framework.

To start with I created an index.php in my root directory which would serve as the entry point to gotfoo.org and a subfolder named ajaxrss where I placed the ajaxagent RSS file:

I haven't inspected all of these files but they work fine so I will focus on the 2 you need to be concerned about.

The agent.php is the main file for ajaxagent where all of the magic happens. This file can be anywhere on your server if you are using more than one of it functions.

The second file rss.php is where you are going to send the URL Feed to be processed.

The function rss get the url and then replaces any white space with "+" to make it url friendly and then it invokes fetch_rss with the url and returns a php list with the rss data, the stats, and any messages. Next it creates an string variable that will contain the rss feed as formatted html. It then creates the $title and $link variables and populates them with the corresponding rss data. Once it has some data it creates a link to the channel it got the rss feed from and inserts the link into the html, then it loops over each item in the the rss feed and adds it as a list-item link. Once it reaches the end of the loop it return the formatted html string back to the calling function.

Here is rss.php w/o comments:


require('rss_fetch.inc');
 
function rss($url)
{
  $url = str_replace(" ", "+", $url);
  list( $rss, $status, $msg) = fetch_rss( $url );
 
    $html .=  "
    "
;   $title = $rss->channel['title']; $link = $rss->channel['link']; $html .= "

$title

"
;   foreach ($rss->items as $item ) { $html .= "
  • "
  • ; $html .= ""; $html .= $item[title]; $html .= ""; } $html .= ""; return $html; } ?>

    You shouldn't need to alter the rss.php file to get this working but if you want to include any other rss data like the description of each feed item or the date you would add it to the foreach loop.

    For example if we wanted to include the description of each feed item we would add this $html .= $item[description]; to the foreach loop:

    foreach ($rss->items as $item ) {
            $html .=   "
  • "
  • ; $html .= ""; $html .= $item[title]; $html .= "
    "
    ; $html .= $item[description]; $html .= ""; }

    To get all of this working all you need to do is add the includes for rss.php and agent.php and the initilize the ajaxagent:

    
      include "ajaxrss/rss.php"; 
     
      include_once("ajaxrss/agent.php"); 
    ?>
     
    
      $agent->init();
    ?>

    The add some JavaScript which calls the call_rss and the callback_rss functions and then I create a window.onload function that invokes call_rss w/ a URL to the rss feed:

    In my Wordpress set up I am using permalink so all of my posts show up as a directory and not a file name or linux.gotfoo.org/?p=123.

    Then add a DIV to place RSS Feed results in:

        
    id='div_rss'>

    and that's it.

    It is important to note that the data is bound to the controls via the ID's. So you'll need to be sure that the DIV you are populating is named div_rss.

    Now when the page loads it fetches the first 10 posts from linux.gotfoo.org and you.gotfoo.org.

    This can easily be used to fetch posts from any RSS feed not just from Wordpress...