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"?> <rdf:RDF> <channel> <title>Freedom Ink</title> <link>http://freedomink.org/</link> <item> <item> <title>First Article</title> <link>http://freedomink.org/node/view/55</link> <description>Short blurb about article........</description> </item> <item> <title>Second Article</title> <link>http://freedomink.org/node/view/11</link> <description>This article shows you how ......</description> </item> </item> </channel> </rdf:RDF>
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.
<?php
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.
<?php
$stories[] = 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.
<?php
$stories[] = 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.
<?php
$data['channel'] = array(
"title" => "Freedom Ink",
"link" => "http://freedomink.org/",
$stories
);
?>
Now we generate the XML using the PEAR XML_Serializer package.
<?php
$serializer = new XML_Serializer($options);
if ($serializer->serialize($data)) {
header('Content-type: text/xml');
echo $serializer->getSerializedData();
}
?>
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.