Tutorial: Create an RSS Feed

Tutorial: Create an RSS Feed – How to use XML_Serializer to build an RSS Newsfeed


This tutorial has been written by cnb and been published on http://freedomink.org/node/62. The site is offline since 2005 and has been rescued using the Internet Archive's Wayback Machine.

Here's how you can create an XML news feed in just five minutes using PHP, PEAR and the PEAR XML_Serializer package by Stephan Schmidt.

What a news feed consists of in it's barest form is your "site name", "site url" and a "listing of stories" from your site. To create a news feed what we need to do is create a publically accessible (web) document which can show this information in a format other sites can understand. This is possible by creating this document in the standard RDF Site Summary (RSS) format. A format used by many sites.

What we are aiming at is creating a document which looks like the following:

This isn't "well formed" XML but a stripped version making it as simple as possible for illustrative purposes. Once we understand the basics we can move on to create well formed XML.

<?xml version="1.0" encoding="UTF-8"?>
        <title>Freedom Ink</title>
                <title>First Article</title>
                <description>Short blurb about article........</description>
                <title>Second Article</title>
                <description>This article shows you how ......</description>

Do it in PHP

Here's an easy way to do it in PHP.

Prerequisite: Install PEAR and the XML_Serializer package.

Include the following default options at the top of your php page.

require_once 'XML/Serializer.php';

$options = array(
"indent"    => "    ",
"linebreak" => "\n",
"typeHints" => false,
"addDecl"   => true,
"encoding"  => "UTF-8",
"rootName"   => "rdf:RDF",
"defaultTagName" => "item"

Let us first create an array containing a listing of the stories on your site.

We create array $stories and add one story.

[] = array(
'title'       => 'First Article',
'link'        => 'http://freedomink.org/node/view/55',
'description' => 'Short blurb about article........'

You can add more stories to the array in the same manner. Ideally this should be done in a "for" or "foreach" loop.

[] = array(
'title'       => 'Second Article',
'link'        => 'http://freedomink.org/node/view/11',
'description' => 'This article shows you how ......'

Finally we specify the channel details and add the stories to it.

['channel'] = array(
"title" => "Freedom Ink",
"link"  => "http://freedomink.org/",

Now we generate the XML using the PEAR XML_Serializer package.

= new XML_Serializer($options);

if (
$serializer->serialize($data)) {
header('Content-type: text/xml');

And that's it! When a person visits the page containing this code an XML file like the one we saw above will be generated and served.

To learn how to parse XML/RSS files you can read this related article which has by far the easiest method of parsing XML/RSS documents.

Example for the usage of XML_Serializer (Previous) XML_Unserializer - class to unserialize (read) XML documents (Next)
Last updated: Fri, 19 Jan 2018 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.