logo

Sue Feng Design

‹ Back to blog

Creating a PHP RSS Feed Page for Your Blog

RSS or Really Simple Syndication is useful for grabbing the latest published content for a web site, such as a blog or news site. When used in a feed reader such as Google Reader, or Bloglines, RSS saves time for people who want to stay updated on sites they visit, as they can view the updates all in one place. Most blogging sites, or platforms such as WordPress have built in RSS feeds. But if you create a site from scratch, this article may be useful. I’ll show you how to create a PHP RSS feed.

You’ll want to already have blog entries on your web site stored on a database. Since this tutorial is PHP based, your server should have PHP support.

This RSS feed is created using PHP but outputs XML or Extensible Markup Language, which is used in generating feeds.

Alright, let’s get started.

1. Map out the structure of the feed.

Take this time to plan out the layout of your feed.

Tags to consider in the structure:

XML – contains the version and encoding of your xml file

RSS – contains the version of your rss as well as name spaces if desired

rss namespace – optional but provides more usable features than the basic feed of title-link-description

channel – describes the RSS feed, has three required elements mentioned next

  • title – title of the channel
  • link – url to the channel
  • description – describes what the channel is about

Here’s an example of the heading so far:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
     <title>Name of Feed</title>
     <link>http://mysite.com</link>
      <description>My blog entries about random stuff</description>
   </channel>
</rss>

language – language the articles are written in (en-us for American English)

standard language codes

image – allows an image to be displayed when a feed is presented

  • title – text to display if image cannot be seen
  • url – url to the image
  • link – url to the website
  • width – width of image
  • height – height of image

item – defines an article in that channel

  • title – title of article
  • link – url to article
  • description – description of the article or short excerpt

Here’s a basic XML file format with minimum elements.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">

<channel>
  <title>Name of Feed</title>
  <link>http://mysite.com</link>
  <description>My blog entries about random stuff</description>
  <item>
    <title>Day 1</title>
    <link>http://mysite.com/entry?id=2</link>
    <description>Today will be an exciting day. I have it all planned, starting with...</description>
  </item>
</channel>

</rss>

See this also, for an example of the way these elements and more are used together for a more detailed feed. Take note that the xml version is not displayed. It is in the source code however.

2. Establish a database connection.

You need to connect your feed page to your database so that you can grab your published entries and print them to your rss.php file. I put comments in as a guide.

// connection information - change string values based on your own values<br />
$host = "localhost";
$user = "username";
$pass = "password";
$database = "databasename";
<br />
// establish the connection and select the database
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

// change the query below so "blog" reflects whatever table you're extracting from. Change "LIMIT" number to the number of recent entries you want to publish.
$query = "SELECT * FROM blog ORDER BY timestamp DESC LIMIT 5";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

3. Put it all together.

This part is a bit tricky to explain concisely, but you can feel free to download this information and edit to your liking. I put comments in it so you can see what’s going on. Basically it’s putting the stuff together in PHP, tags and tag contents, and the file prints out as XML. The PHP is used so content can be pulled from the database and the file content stays current, based on how frequently you publish articles.

View and download file here.

If you have questions, feel free to ask!

Posted on: September 15, 2011Categories: TutorialsTags: coding
‹ Back to blog