• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

RSS feeds in PHP: 3 simple steps to PHP RSS generation

By Akash Mehta | on Apr 17, 2008 | 1 Comment
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

Syndication is well and truly here; just about every major content site has an RSS feed, the leading web browsers have inbuilt RSS readers and there are entire startups dedicated to helping you keep track of the sites that aggregate your feeds – let alone those that actually do the feed reading. Adding an RSS feed to your site is quick and painless, and can be done with pure PHP (and some kind of data source). After the jump, 3 simple steps to building an RSS feed in php.

Step 1: Find your content.
An RSS feed should probably list all the content items in your main section. For example, if you run a blog, you can have an RSS feed of all your latest posts. This blog does. If you run a small download site, consider a feed of your latest releases. Whatever content you choose, make sure it’s interesting, and will be updated regularly – aim for between 3-6 times a week, if you have a choice. Then just write out your basic database logic and get the data into an array. I’ll use $items for this example.

Step 2: Get your basic RSS structure.
RSS is an XML-based format, so all you really need is a set of XML tags as part of the RSS specification. RSS provides a number of optional tags, and being XML, it can be (and often is) extended with any other tag. For example, the Dublin Core metadata is a common inclusion. You can borrow the basic structure from any feed, including ours (load it up and right click > view source), but here’s a sample you can use:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
  <title>My Blog Name</title>
  <link>http://example.com/blog</link>
  <description>Keeping webmasters up-to-date on technology.</description>
  <pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
  <item>
    <title>Story Title</title>
    <pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
    <link>http://example.com/my/story</link>
    <description>Today I saw a whale!</description>
  </item>
</channel>
</rss>

The easiest way to build your RSS feed in PHP is to leave all of that XML as plain text and only loop over the sections as needed (the <item> tags). The one gotcha here, as you’ve probably noticed, is the opening <?xml tag. This will probably clash with PHP, so instead echo that line out:

<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>

Then your structure is all in place. The item element is repeated for each story we have in our RSS feed; the date is RFC 2822 formatted (just use date("r")). We’ll use a simple foreach loop to put the content in:

<?php foreach ($items as $item) { ?>
  <item>
    <title><?=$item['title']?></title>
    <pubDate><?=date("r",$item['time'])?></pubDate>
    <link><?=$item['url']?></link>
    <description><?=$item['description']?></description>
  </item>
<?php } ?>

3. Serve it up!
To be recognised as an RSS feed, you’ll need to serve it as the right content type. Technically you should use application/rss+xml, but most browsers won’t recognise this. I find application/xhtml+xml works well, as does text/xml. The choice is yours, although IE7 seems to like text/xml better.

<?php header("Content-type: text/xml"); ?>

And we’re done!

Here’s a sample of the final product:

<?php header("Content-type: text/xml"); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>
<rss version="2.0">
<channel>
  <title>My Blog Name</title>
  <link>http://example.com/blog</link>
  <description>Keeping webmasters up-to-date on technology.</description>
  <pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
<?php foreach ($items as $item) { ?>
  <item>
    <title><?=$item['title']?></title>
    <pubDate><?=date("r",$item['time'])?></pubDate>
    <link><?=$item['url']?></link>
    <description><?=$item['description']?></description>
  </item>
<?php } ?>
</channel>
</rss>
Share this story:
  • tweet

Tags: create rssphp content syndicationphp rss feed

Author Description

One Response to “RSS feeds in PHP: 3 simple steps to PHP RSS generation”

  1. April 19, 2008

    wcoenen Log in to Reply

    Generating plain text is a sure way to end up with something that looks like XML but is not well-formed. For example, what if the title or descriptions contain characters like & that need to be escaped?

    I learned this the hard way: if it’s going to be read by an XML parser, then you should generate it with something like DOMDocument or XML writer. Especially if you don’t control the clients that are going to read your “XML”.

    An even better choice would be to not reinvent the wheel: http://freshmeat.net/projects/rsswriterclass/

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 0Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.