EG4d0

How to Create Custom RSS Feed Using MySQL and PHP

A RSS (Really Simple Syndication) feed is a XML-based format for your content. Most blogging platforms, for example, will have an RSS feed built in. Whenever you start publishing posts, your latest posts will be updated in the RSS feed. Visitors to your website can subscribe to your blog’s RSS feed in an RSS reader [...]

RSS

A RSS (Really Simple Syndication) feed is a XML-based format for your content. Most blogging platforms, for example, will have an RSS feed built in. Whenever you start publishing posts, your latest posts will be updated in the RSS feed. Visitors to your website can subscribe to your blog’s RSS feed in an RSS reader such as Google Reader.


Tutorial Details

  • Program: Notepad / Notepad++
  • Version (if applicable): PHP , MYSQL
  • Difficulty: Beginner
  • Estimated Completion Time: 30-50 Minutes

Defining a Goal

Our goal is to create RSS Feed System with a database.

Download

Lets split the processes we are going to do in this tutorial.

  1. Create the database
  2. Create the index page for the RSS items
  3. Create the connector for the database connection
  4. Create the class for getting information of the RSS items

Step 1: Create the database.

We will create two database tables, the first is called rss_details, which contains the details for the feed and the second is called rss_info, which contains all of the items.

Note: Make sure you have created a database to be used with our RSS System and put the configuration parameters in step 3 of the tutorial.

Create a table in the database and name it rss_details. Then give the table the fields shown in Table-Img.

Create this simple table with this MySQL table query by the following CREATE TABLES statement:

CREATE TABLE IF NOT EXISTS `rss_details` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` text NOT NULL,
  `descr` text NOT NULL,
  `url` text NOT NULL,
  `image_info` text NOT NULL,
  `image_url` text NOT NULL,
  `image_source` text NOT NULL,
  `image_width` text NOT NULL,
  `image_height` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

We need to create a second table in the database and name it rss_info. Then give the table the fields shown in  Table-Img.

Create this simple table with this MySQL table query by the following CREATE TABLES statement:

CREATE TABLE IF NOT EXISTS `rss_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` text NOT NULL,
  `descr` text NOT NULL,
  `url` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

 


Step 2: Create the index page

In the index.php file, we start by adding a header that will configure the content type of the document as valid XML and choose the charset that you need.
Include the class.php, instantiate the object and trigger a method called GetFeed.
The GetFeed method of a table delegate class or application delegate class returns an associative array of parameters to configure the RSS feed.

Create this simple index page with my write source:

     <?
      header("Content-Type: application/xml; charset=ISO-8859-1");
      include("class.php");
      $rss = new RSS();
      echo $rss->GetFeed();
     ?>

 


Step 3: Create connector for the database.

The connector.php file defines our database connection information, makes the initial connection and selects the defined database.

     <?
      DEFINE ('DB_USER', 'your_name');
      DEFINE ('DB_PASSWORD', 'your_password');
      DEFINE ('DB_HOST', 'localhost');
      DEFINE ('DB_NAME', 'your_databasename');
      //connection to the database
      $dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
      mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
    ?>

 

 


Step 4: Create the class for getting information of the RSS items

The RSS class constructor is a public method that requires the connector.php file. Remember to put this file in a safe location on your server so it’s not exposed to hackers. Once the file’s in place change the path in the file index.php on line 3.

<?
  class RSS
  {
    public function RSS()
    {
        require_once ('connector.php');
    }
    public function GetFeed()
    {
        return $this->getDetails() . $this->getItems();
    }
    private function dbConnect()
    {
        DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
    }
    private function getDetails()
    {
        $detailsTable = "rss_details";
        $this->dbConnect($detailsTable);
        $query = "SELECT * FROM ". $detailsTable;
        $result = mysql_db_query (DB_NAME, $query, LINK);
        while($row = mysql_fetch_array($result))
        {
            $details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
                <rss version="2.0">
                    <channel>
                        <title>'. $row['title'] .'</title>
                        <link>'. $row['link'] .'</link>
                        <description>'. $row['description'] .'</description>
                        <language>'. $row['language'] .'</language>
                        <image>
                            <title>'. $row['image_title'] .'</title>
                            <url>'. $row['image_url'] .'</url>
                            <link>'. $row['image_link'] .'</link>
                            <width>'. $row['image_width'] .'</width>
                            <height>'. $row['image_height'] .'</height>
                        </image>';
        }
        return $details;
    }
    private function getItems()
    {
        $itemsTable = "rss_info";
        $this->dbConnect($itemsTable);
        $query = "SELECT * FROM ". $itemsTable;
        $result = mysql_db_query (DB_NAME, $query, LINK);
        $items = '';
        while($row = mysql_fetch_array($result))
        {
            $items .= '<item>
                <title>'. $row["title"] .'</title>
                <link>'. $row["link"] .'</link>
                <description><![CDATA['. $row["description"] .']]></description>
            </item>';
        }
        $items .= '</channel>
                </rss>';
        return $items;
    }
}
?>

Download

Congratulations, you finished your Custom RSS Feed.

Related posts:

  1. Create Ajax Search Using PHP MYSQL and jQuery
  2. How To Export MySql To CSV ?
  3. Create simple wordpress plugin From Scratch Part-2