My Blog

Create RSS Feeds with Zend Framework

3.25.2011 | Blog, PHP, Zend Framework

In this post I’ll introduce how to create RSS feeds using Zend Framework through Zend_Feed Component.

We have got some mandatory elements in the channel, and the articles. Every Channel has to have the title, description, link and description elements. The articles also have some mandatory fields, as title, link, GUID (Globally Unique Identifier) and you can import as optional parameters as you want.

First, We’ll create a controller for the RSS, we’ll name it, rssController.php and create the method for the action, which by default will be the index action

class RssController extends Zend_Controller_Action {
	/**
	 * The default action for the rss controller
	 * Which shows the rss document
	 */
	public function indexAction() {
		// method body
	}
}

I’ll use sample data in an array for the ease of this tutuorial, you will normally retrieve these data from your database.
We’ll Assume that we have got 2 articles to syndicate:

$feedData = array(
			'title'=>'Channel Title',
			'description'=>'Channel Description here',
			'link'=>'http://www.example.com',
			'charset'=>'utf8',
			'entries'=>array(
				array(
					'title'=>'Article 1',
					'description'=>'Article 1 contents goes here',
					'link'=>'http://www.example.com/article-1',
					'guid'=>'1A'
				),
				array(
					'title'=>'Article 2',
					'description'=>'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
					'link'=>'http://www.example.com/article-2',
					'guid'=>'2A'
				)
			)
		);

We have got 2 methods of generating the RSS document. The first is using the Zend_Feed’s importArray method and the other one using the importBuilder, I’ll explain how to use both in the following snippet.

importArray accepts 2 parameters, first is the data array and the second is the format(string) with ‘rss’ or ‘atom’ defaults to ‘atom’

feed = Zend_Feed::importArray($feedData,'rss');

importBuilder method accepts 2 parameter, first Zend_Feed_Builder object and the second is the feed format(string) either ‘rss’ or ‘atom’ defaults to ‘atom’.

$feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($feedData),'rss');

Then we’ll set the content type of the document to be xml

header('Content-type: text/xml');

After that we’ll generate and print the rss XML document we are building using send() method which returns the generated xml document.

echo $feed->send();

Finally, we need to suppress the view, because we need no view for this action:

$this->_helper->viewRenderer->setNoRender();

Now let’s wrap up all the code,

// uncomment this if you have autoloading disabled.
// require_once 'Zend/Controller/Action.php';

class RssController extends Zend_Controller_Action {
	/**
	 * The default action for the rss controller
	 * Which shows the rss document
	 */
	public function indexAction() {
		$feedData = array(
			'title'=>'Channel Title',
			'description'=>'Channel Description here',
			'link'=>'http://www.example.com',
			'charset'=>'utf8',
			'entries'=>array(
				array(
					'title'=>'Article 1',
					'description'=>'Article 1 contents goes here',
					'link'=>'http://www.example.com/article-1',
					'guid'=>'1A'
				),
				array(
					'title'=>'Article 2',
					'description'=>'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
					'link'=>'http://www.example.com/article-2',
					'guid'=>'2A'
				)
			)
		);
		// create our feed object and import the data
		$feed = Zend_Feed::importArray ( $feedData, 'rss' );
		
		// set the Content Type of the document
		header ( 'Content-type: text/xml' );
		
		// echo the contents of the RSS xml document
		echo $feed->send()
	}
}

Now, let’s test it using this url: http://example.com/rss/

You should get this output:

<?xml version="1.0" encoding="utf8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  <channel>
    <title><![CDATA[Channel Title]]></title>
    <link>http://www.example.com</link>

    <description><![CDATA[Channel Description here]]></description>
    <pubDate>Fri, 25 Mar 2011 20:42:58 +0200</pubDate>
    <generator>Zend_Feed</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <item>
      <title><![CDATA[Article 1]]></title>
      <link>http://www.example.com/article-1</link>

      <guid isPermaLink="false">1A</guid>
      <description><![CDATA[Article 1 contents goes here]]></description>
      <pubDate>Fri, 25 Mar 2011 20:42:58 +0200</pubDate>
    </item>
    <item>
      <title><![CDATA[Article 2]]></title>
      <link>http://www.example.com/article-2</link>

      <guid isPermaLink="false">2A</guid>
      <description><![CDATA[Lorem ipsum dolor sit amet, consectetur adipiscing elit.]]></description>
      <pubDate>Fri, 25 Mar 2011 20:42:58 +0200</pubDate>
    </item>
  </channel>
</rss>

That was easy, right?

Happy Coding

Be Sociable, Share!

Responses

palmplam
3.29.2011

Instead of :
// set the Content Type of the document
header(‘Content-type: text/xml’);

// echo the contents of the RSS xml document
echo $feed->saveXML();

You can directly use:
$feed->send();

In this case, no need to change your code if you want to change your feed Type 😉

Omar Shaban
3.31.2011

yea you’re right its more portable, but practically who changes the feed type?

Alex
8.16.2011

The feed would generate an error in IE until I added a dash to charset:

‘charset’=>’utf8’

changed to

‘charset’=>’utf-8’

bijul
8.18.2011

thanks omer your code is very useful to understand rssfeed

suvi
3.16.2012

What about using Zend_Feed_Writer_Feed? Would that not be more straight forward?

jank
11.01.2012

Hi,

thanks, very nice.
How can i set a date?

Michal Voják
12.17.2012

Thanks to Alex, that was very helpful for me.

Morray
2.23.2013

Hello

Thanks for the tutorial, i found it use full. keep writing.

Comments