PHP

Buy me a beer or a book?

amazon PayPal - The safer, easier way to donate online!

Dynamic RSS feed using PHP and MySQL

Recently I have been working a lot with RSS feeds, and they are becoming a more globally used plugin or addon onto any website, I regularly use premade plugins for CMS's such as Joomla or Wordpress to enabled RSS feeds to be automatically be added to posts, dependant upon categories or categories. Anyway, it is quite bland out there as to the stages you need to perform in order to make a custom RSS feed for yourself which is standards compliant and fully customisable from an admin panel. Here is a class I came up with which allows multiple feeds to be generated by the same file.

  1. You will need a minimum of 2 tables in a database, 1 containing the details of the feeds available, and another table for each of those individual feeds.
    The feeds table should look like this:  "|  ID (key)  |  feed_title  |  feed_name  |  feed_url  |  feed_description  |  feed_language  |"
  2. You will need a file called rss.php (this will be used to call the main class, it will look like this:

    rss.php
    1
    2
    3
    4
    5
    include_once('class.rss.php');
     
    $rss_feed_output = new RSS($_GET['id']);
     
    echo $rss_feed_output

  3. The main class.rss.php is shown below, as you can see it first grabs info about the RSS feed requested, and then it parses the feed.
    class.rss.php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
     
     
    class RSS
    {
    var $sql;
    var $query;
     
    var $db_host = 'hostname';
    var $db_user = 'username';
    var $db_pass = 'password';
    var $db_name = 'db_name';
     
    //This works like a __construct function, but this way of
    //performing it is compatible with PHP 4 and PHP 5
    function RSS()
    {
    mysql_connect($this->db_host,
    $this
    ->db_user,$this->db_pass);

    mysql_select_db($this->db_name);
    }
     
    function get_feed()
    {
    //runs the functions which retrieve the RSS feed
    return $this->getDetails();
    }
     
    function get_details($id)
    {
    //performs the SQL necessary to grab the details
    // of the feed which is to be displayed.
    $this->sql = 'SELECT * FROM jos_myfeeds WHERE id='. $id;
    $this->query = mysql_query($this->sql)
    or die('query failed [get_details() - class.rss.php]: '.
    mysql_error());
    $this->row = mysql_fetch_array($this->query);
     
    //After retrieving the details of the selected feed
    //it sets the encoding and declares the page as
    //XML/RSS and then places the title information
    $this->details = '‹?xml version="1.0"'.
     'encoding="ISO-8859-1" ?›';
    $this->details .= 'rss version="2.0"›';
    $this->details .= 'channel›';
    $this->details .= 'title›'.
    htmlentities($this->row['feed_title'])
    .'
    /title';
    $this->details .= '‹link'.
    htmlentities($this->row['feed_url'])
    .'
    ‹/link';
    $this->details .= 'description›'.
    htmlentities($this->row['feed_description'])

    .'
    ‹/description›';
    $this->details .= '‹language'.
    htmlentities($this->row['feed_language'])
    .'
    ‹/language›';
     
    //this calls the function to fetch the items from
    //the particular feed

    $this->details .= $this->get_items($this->row['feed_name']);
     
    //returns a fully compliant RSS feed
    return $this->details;
    }
     
    function get_items($feed_name)
    {
    //performs the SQL necessary to grab the feeds of
     //the RSS feed which is to be displayed.
    $this->sql = 'SELECT * FROM rss_'.$feed_name;
    $this->query = mysql_query($this->sql)
    or die('query failed [get_items() - class.rss.php]: '.
    mysql_error());

    $items = '';
     
    //loops through all of the RSS feeds to dynamically
    //display them

    while($row = mysql_fetch_array($this->query))
    {
    $items .= '‹item›title'.
    htmlentities($row['title']) .'
    /title'.
    '
    link›'.
    htmlentities($row['url']).
    '‹/link›description›'
    htmlentities($row['introtext']-->) .']]>
    ‹/description';
    ‹/item'
      }
    //closes the remainder of the syntaxing for the RSS version
    $items .= '/channel/rss';
     
    //returns the XML/RSS for all the feeds
    return $items;
    }
     
    }
     

  4. Finishing touch is to add .htaccess so your file looks to the user as if you have www.mydomain.com/rss_feed.xml the following htaccess assumes the position of your rss.php being in the same base directory of your website, if not then you will need to amend the root of the file to make it relative:

    .htaccess
    1
    2
    RewriteEngine on
    RewriteRule ^rss_feed.xml rss.php [L]

Free Downloads