October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating a Custom RSS Feed Aggregator

  • November 26, 2003
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

The PHP Script

The final task is to create the script that will display the user's chosen RSS feeds. This script, titled myrss.php, is surprisingly simple, consisting of just two main components:

  • Login: The user will need to log in to view his customized feeds. This is accomplished by using a simple form and some logic that will confirm the supplied e-mail address and password.
  • RSS Aggregation and Delivery: Assuming a valid login (or valid session; see next paragraph), we'll use the Magpie RSS Parser to display the user's chosen RSS feeds.

To save users the hassle of having to repeatedly log in every time they need an RSS "fix," I make use of PHP's session-handling feature. Once successfully logged in, the user's unique identifier is stored as a session variable for later retrieval. Keep in mind that the storage duration is entirely dependent upon how your PHP installation's session functionality is configured.

Listings 1-1 and 1-2 offer the commented code for both components. Both components are assembled and available for viewing via a link posted at the conclusion of Listing 1-2.

Listing 1-1: The Login Component

// Start or continue a session

session_start();


// Has the user

if (! isset($_SESSION['userid']))
{

   if (! isset($_POST['email']))
   {

      echo "<form action='myrss.php' method='post'>";
      echo "Email:<br />";
      echo "<input type='text' name='email' size='20'
                   maxlength='55' value='' /><br />";
      echo "Password:<br />";
      echo "<input type='password' name='pswd' size='20'
                   maxlength='20' value='' /><br />";
      echo "<input type='submit' value='login'>";
      echo "</form>";

   } else {

      mysql_connect("localhost","aggregator","secret");
      mysql_select_db("rssfeeds");

      $email = $_POST['email'];
      $pswd = md5($_POST['pswd']);

      $query = "SELECT rowID, email, pswd FROM user 
                WHERE email='$email' AND '$pswd'";
      $result = mysql_query($query);

      if (mysql_numrows($result) != 1)
      {
         echo "<p>Could not login!</p>";
      } else {
         list($rowID, $email, $pswd) = mysql_fetch_row($result);
         $_SESSION['userid'] = $rowID;
      }

      mysql_close();

   } // end isset[email]

}

Listing 1-2. The RSS Aggregation and Delivery Component

if (isset($_SESSION['userid'])) {

   require_once("rss_fetch.inc");

   mysql_connect("localhost","aggregator","secret");
   mysql_select_db("rssfeeds");	

   $userID = $_SESSION['userid'];

   $query = "SELECT rss.title, rss.url
             FROM rssfeed AS rss, user_to_rss_feed as userrss
             WHERE userrss.userid='$userID' 
             AND rss.rowID=userrss.rssid";

   $result = mysql_query($query);

   while(list($title,$url) = mysql_fetch_row($result)) 
   {

      $url = "$url";
      $rss = fetch_rss( $url );

      echo "<strong>" . $rss->channel['title'] . "</strong><p>";
      echo "<ul>";
      foreach ($rss->items as $item) 
      {
         $href = $item['link'];
         $title = $item['title'];
         echo "<li><a href=$href>$title</a></li>";
      }

      echo "</ul>";

   }

   mysql_close();

}

View the complete script here

Figure 1-1 depicts the login form.

Figure 1-2 depicts a typical display of requested RSS feeds.

Conclusion

I welcome questions and comments! E-mail me at jason@wjgilmore.com. I'd also like to hear more about your experiences integrating RSS feeds with PHP and MySQL!

About the Author

W. Jason Gilmore (http://www.wjgilmore.com/ ) is an Internet application developer for the Fisher College of Business. He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Linux magazine.

# # #





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel