Creating RSS feed

Discussion in 'PHP' started by star2323, Mar 20, 2006.

  1. #1
    I have a site I would like to generate an RSS feed for it. I'm using PHP and mySQL on the site right now. Now I found the the structure for RSS 2.

    
    <?xml version="1.0"?>
    <rss version="2.0">
      <channel>
        <title>Example Channel</title>
        <link>http://example.com/</link>
        <description>My example channel</description>
        <item>
           <title>News for September the Second</title>
           <link>http://example.com/2002/09/01</link>
           <description>other things happened today</description>
        </item>
        <item>
           <title>News for September the First</title>
           <link>http://example.com/2002/09/02</link>
        </item>
      </channel>
    </rss>
    
    Code (markup):
    Right now I populate the homepage with the newest articles. I query the database and then display the article titles, links, etc... To create a RSS feed for the newest articles do I simply generate a file using the above structure and then name the file something and this is my RSS feed? Is it that easy?

    How do I automatically update the RSS feed? Can I put PHP code in the RSS feed in order to query the database and pull the latest articles?
     
    star2323, Mar 20, 2006 IP
  2. woodside

    woodside Peon

    Messages:
    182
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes, you just need to create a file that looks like what you have. You can either make some sort of a schedule task or cron to run you php script that creates the file, or just have it created automatically when your site changes. I do it both ways for different sites.

    Php file examples here

    -Erik
     
    woodside, Mar 20, 2006 IP
  3. star2323

    star2323 Peon

    Messages:
    445
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can the RSS file be a PHP file and simply generate the correct headers so it appears to be an XML file?
     
    star2323, Mar 20, 2006 IP
  4. woodside

    woodside Peon

    Messages:
    182
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I don't do it that way, but I see no reason why it wouldn't work.

    -Erik
     
    woodside, Mar 20, 2006 IP
  5. quaffapint

    quaffapint Active Member

    Messages:
    299
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #5
    You can also use one of the many existing PHP classes out there. I use one (feedcreator) on my arcade site that formats the XML and such for me. I just pass the data to the different pieces through a sql query.
     
    quaffapint, Mar 20, 2006 IP
  6. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can use php file to create the rss/xml feeds and you do need to use the header to output the right format:

    header('Content-type: text/xml'); 
    echo "<" . "?" . "xml version=\"1.0\" encoding=\"ISO-8859-1\"" . "?" . ">";
    PHP:
    then just echo the xml code as normal.
     
    mad4, Mar 21, 2006 IP
  7. star2323

    star2323 Peon

    Messages:
    445
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks everybody.

    All those RSS feed services are not needed at all. Setting up a feed for your site is actually pretty easy.
     
    star2323, Mar 21, 2006 IP