MySQL
  Home arrow MySQL arrow Page 3 - Building a Generic RSS Class With PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Building a Generic RSS Class With PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2002-11-05

    Table of Contents:
  • Building a Generic RSS Class With PHP
  • What is RSS and How Does It Relate to XML?
  • Building Our PHP Class
  • The GetRSS Function
  • Testing Our Class
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a Generic RSS Class With PHP - Building Our PHP Class


    (Page 3 of 6 )

    OOP makes life easier, there's no doubt about that. Programming with objects instead of functions and normal variables is quicker and is also more fun, too. If you're not familiar with OOP in PHP, then have a quick glance over David’s article by clicking here.

    We're going to build a class that will automatically generate a 100% RSS-compliant XML string for us. With this class, you will be able to share and syndicate your site's content and brag to all of your friends that you're down with the new trends in the world of XML.

    Let's start by creating a simple MySQL database, where we will store some fictional articles. Drop to the MySQL command window and run the following commands:

    create database rssDB;
    use rssDB;

    create table myArticles
    (
    articleId int auto_increment primary key,
    title varchar(50),
    summary varchar(250),
    content text,
    unique id(articleId)
    );


    We've just created a database and a simple table that will hold some articles. Remember that each item tag in our RSS XML file needs a title, description and link. The title, summary and articleId fields will be useful for this later on.

    We now need some articles. Run the following commands at your MySQL console to add some records to our myArticles table:

    insert into myArticles values (0, '1st Article', 'This is the summary of my first article', 'This is the body of my first article');

    insert into myArticles values (0, '2nd Article', 'This is the summary of my second article', 'This is the body of my second article');

    insert into myArticles values (0, '3rd Article', 'This is the summary of my third article', 'This is the body of my third article');


    Now, let's start to create our class file. We will build on this code as we go, and a complete version is available in the support material for this article on the last page:

    class myRSS
    {
    // Create our channel variables
    var $channelTitle;
    var $channelLink;
    var $channelDesc;

    // Create our image variables
    var $imageTitle;
    var $imageLink;
    var $imageURL;


    We start by creating our class and calling it myRSS. We also setup 6 class-scoped variables to hold the details that will be used to build our channel and image tags later.

    Our class will be very simple, and will contain 2 functions:

    function checkValues()

    This function will make sure that appropriate values have been set for variables such as the channel's title, link and description, as well as the image details.

    function GetRSS($dbServer, $dbUser, $dbPass, $dbName, $tableName, $titleFieldName, $descFieldName, $linkFieldName, $linkTemplate)

    This function is a little more complex. It accepts your MySQL database details, table details and field details and builds an RSS XML string, which it returns.

    Let's start by looking at the checkValues function:

    function checkValues()
    {
    // Make sure all channel and image values are set
    if($this->channelTitle == "")
    die("Please specify a channel title");

    if(ereg("$http://", $this->channelLink) == false)
    die("Please specify a channel link");

    if($this->channelDesc == "")
    die("Please specify a channel description");

    if($this->imageTitle == "")
    die("Please specify an image title");

    if(ereg("$http://", $this->imageLink) == false)
    die("Please specify an image link");

    if(ereg("$http://", $this->imageURL) == false)
    die("Please specify an image URL");
    }


    The checkValues function uses some simple string comparison operators and some regular expressions to make sure class variables aren't empty, and that URL's for the image location, etc are prefixed with http://. This function is called from the GetRSS function, which is the main function of our application.

    Before we look at the GetRSS function, let's look at how we call our class, assuming that it's complete and all functions are setup. From a new file called test.php, we could use the following code:

    include("class.myRSS.php");

    // Instantiate the myRSS class
    $myRSS = new myRSS;

    $myRSS->channelTitle = "My sample channel";
    $myRSS->channelLink = "http://www.mysite.com";
    $myRSS->channelDesc = "My sample RSS XML channel";

    $myRSS->imageTitle = "My sample channel";
    $myRSS->imageLink = "http://www.mysite.com/mylogo.gif";
    $myRSS->imageURL = "http://www.mysite.com";

    // Get the RSS data
    $rssData = $myRSS->GetRSS("localhost", "admin", "password", "rssDB", "myArticles", "title", "summary", "articleId", "http://www.mysite.com/articles/{linkId}/");

    // Output the generated RSS XML
    header("Content-type: text/xml");
    echo $rssData;


    There's nothing fancy about this code. We start by setting the appropriate values for the channel and image attributes, as discussed earlier. We then call the GetRSS function to grab our RSS XML and set a header type to tell the browser we're outputting XML instead of plain text.

    Before we look at the GetRSS function, here's a sneak peak of the XML output in IE 6.0:

    A preview of our RSS XML in IE6.0

    More MySQL Articles
    More By Mitchell Harper


     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT