Cre­at­ing a sim­ple RSS feed in PHP

rss-bannerIn my cur­rent pro­ject, #tifnum, I wanted to add a sim­ple RSS feed, so that in­ter­ested peo­ple can get the newest fa­vorite num­ber every day. Af­ter try­ing out many so­lu­tions, I found out that us­ing the Sim­pleXM­LEle­ment it is one sim­ple way of do­ing this. As­sum­ing that you have an ar­ray con­tain­ing your ar­ti­cles, e.g. like this:

$entries = array(
    array(
        "title" => "My first test entry",
        "description" => "This is the first article's description",
        "link" => "http://leolabs.org/my-first-article-url"
    ),
    array(
        "title" => "My second test entry",
        "description" => "This is the second article's description",
        "link" => "http://leolabs.org/my-second-article-url"
    ),
    array(
        "title" => "My third test entry",
        "description" => "This is the third article's description",
        "link" => "http://leolabs.org/my-third-article-url"
    )
);
Now creating the RSS feed is fairly simple. First you have to tell the browser that the content you want to deliver is a RSS feed, then you create the SimpleXMLElement and tell the RSS reader that you're using the RSS version 2.0:

header("Content-Type: application/rss+xml; charset=UTF-8");

$xml = new SimpleXMLElement('<rss/>'); $xml->addAttribute("version", "2.0"); First, let's see what a nor­mal RSS feed looks like:

<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0">

<channel> <title>Your feed ti­tle</title> <link>Your web­site's uri</link> <description>De­scribe your feed</description> <language>en-us</language>

<span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Your entry's title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">description</span>&gt;</span>Your entry's description<span class="hljs-tag">&lt;/<span class="hljs-name">description</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span>&gt;</span>http://my-site.com/article<span class="hljs-tag">&lt;/<span class="hljs-name">link</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">item</span>&gt;</span>
  ...
<span class="hljs-tag">&lt;/<span class="hljs-name">item</span>&gt;</span>

</channel> </rss> As you can see, the RSS feed con­tains an el­e­ment called which con­tains every­thing the RSS reader needs to know, in­clud­ing the ar­ti­cles, which are stored in el­e­ments called . First, we'll add the basic information elements to your feed:

$channel = $xml->addChild("channel");

$channel->ad­d­Child("title", "Your feed title"); $channel->ad­d­Child("link", "Your website's uri"); $channel->ad­d­Child("description", "Describe your feed"); $channel->ad­d­Child("language", "en-us"); Now, we can add en­tries from the ar­ray to this feed:

foreach ($entries as $entry) {
    $item = $channel->addChild("item");

$item-&gt;addChild(<span class="hljs-string">"title"</span>, $entry[<span class="hljs-string">'title'</span>]);
$item-&gt;addChild(<span class="hljs-string">"link"</span>, $entry[<span class="hljs-string">'link'</span>]);
$item-&gt;addChild(<span class="hljs-string">"description"</span>, $entry[<span class="hljs-string">'description'</span>]);

} Now that every­thing's done, you can just out­put the feed us­ing the de­fault echo com­mand:

echo $xml->asXML();
And that's it. You now have your own simple RSS feed. If you want to, you can add more information to the feed, like information about the author or the article's date. You can get more information about that in this Wikipedia article or here. I love using CodeIgniter to create my PHP projects. If you want to create your RSS feed dynamically using a MySQL table, like

title description link
My first test entry This is the first article's description http://site.org/first-article
My second test entry This is the second article's description http://site.org/second-article
My third test entry This is the third article's description http://site.org/third-article

you can do this by us­ing CodeIgnit­er's ac­tive record class:

$entries = $this->db->get('my_articles')->result_array();
If you have any questions or suggestions concerning this method, just leave them in the comments below this article.