PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » PHP » RSS Generator

RSS Generator

You'll need a MySQL database with a a table called `rss_feed`. In that table there are 3 colums: item title (which is the name a person will see for an item), the item link (which is the location of the page with the item on it), and description (which tells what the feed is about). Put the file in a folder called feed and you're feed will be available at yoursite.com/feed

Remember to change the feed title, link and image for your specific feed.

<?php

   // Connect to database... (you'll need to create this yourself)
   require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/connection.php';

   // Run query...
   $getFeed = mysql_query("SELECT *
                           FROM `rss_feed`
                           ORDER BY `time` DESC
                           ")or die(mysql_error());

   // Output XML (RSS)
    echo '<?xml version="1.0" encoding="ISO-8859-1" ?>
          <rss version="2.0">
                <channel>
                        <title>Your RSS Title</title>
                        <link>http://the_location_of_your_feed/feed</link>
                        <description>Description of your Feed</description>
                        <language>English</language>
                        <image>
                                <title>website Logo</title>
                                <url></url>
                                <link>Link to image</link>
                                <width>width</width>
                                <height>height</height>
                        </image>';
						while($rssFeed = mysql_fetch_array($getFeed)) {
        					 echo '<item>',
					              '<title>', $rssFeed['item_title']</title>',
					              '<link>', $rssFeed['link'], '</link>',
					              '<description><![CDATA[ ,$rssFeed['description'],']]></description>
								   </item>';

 						}
				echo '</channel>
        </rss>';

?>

Subscribe to The Thread

  1. Dan says:

    PHP Code for the mysql connection:

    mysql_connect(‘host’, ‘username’, ‘password’);
    mysql_select_db(‘dbname’);

  2. Frederik says:

    Hi,

    I can’t seem to get this to work… When I get the feed in my reader, if just output this:

    ”, $rssFeed['item_title']‘,

    instead of the actual title.

    I’m using Coda and when I place the script it looks as if everything up until “” is commented out.. (green text).

    Got an idea what I’m doing wrong?

    Thanks

    /Frederik

  3. Frederik says:

    OK got it working now. I had to give the file a .php suffix, and send a content-type header to serve it up as XML. Also it seemed there was some syntaxt errors in the echo, so I replaced some “,” with some “.” and added some “‘”

    Here’s what I got:

    <?php
    //Parse the php file as xml
    header('Content-Type: text/xml');
    
    //incude database connection info
    include('../inc/db.php');
       // Run query...
       $getFeed = mysql_query("SELECT * FROM `rss_feed` ORDER BY id DESC")or die(mysql_error());
    
       // Output XML (RSS)
        echo '
    
                            Your RSS Title
                            http://the_location_of_your_feed/feed
                            Description of your Feed
                            English
    
                                    website Logo
    
                                    Link to image
                                    width
                                    height
                            ';
    						while($rssFeed = mysql_fetch_array($getFeed)) {
            					 echo ''.
    					              ''.$rssFeed['item_title'].'',
    					              ''.$rssFeed['link']. '',
    					              '
    								   ';
    
     						}
    				echo '
            ';
    
    ?>
  4. Stupid question. I created this and my pages aren’t actually showing up on the rss feed. Anyone know why OR how I can make them start to show up in there?
    http://kissweddingguide.com/feed.php

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~