Consuming an RSS Feed

Reading a feed

Reading an RSS feed is as simple as passing the URL of the feed to Zend\Feed\Reader\Reader‘s import method.

1
$channel = Zend\Feed\Reader\Reader::import('http://rss.example.com/channelName');

If any errors occur fetching the feed, a Zend\Feed\Reader\Exception\RuntimeException will be thrown.

Get properties

Once you have a feed object, you can access any of the standard RSS “channel” properties directly on the object:

1
echo $channel->getTitle();

Properties of the channel can be accessed via getter methods, such as getTitle, getAuthor ...

If channel properties have attributes, the getter method will return a key/value pair, where the key is the attribute name, and the value is the attribute value.

1
2
$author = $channel->getAuthor();
echo $author['name'];

Most commonly you’ll want to loop through the feed and do something with its entries. Zend\Feed\Reader\Feed\Rss internally converts all entries to a Zend\Feed\Reader\Entry\Rss. Entry properties, similarly to channel properties, can be accessed via getter methods, such as getTitle, getDescription ...

An example of printing all titles of articles in a channel is:

1
2
3
foreach ($channel as $item) {
    echo $item->getTitle() . "\n";
}

If you are not familiar with RSS, here are the standard elements you can expect to be available in an RSS channel and in individual RSS items (entries).

Required channel elements:

  • title- The name of the channel
  • link- The URL of the web site corresponding to the channel
  • description- A sentence or several describing the channel

Common optional channel elements:

  • pubDate- The publication date of this set of content, in RFC 822 date format
  • language- The language the channel is written in
  • category- One or more (specified by multiple tags) categories the channel belongs to

RSS <item> elements do not have any strictly required elements. However, either title or description must be present.

Common item elements:

  • title- The title of the item
  • link- The URL of the item
  • description- A synopsis of the item
  • author- The author’s email address
  • category- One more categories that the item belongs to
  • comments-URL of comments relating to this item
  • pubDate- The date the item was published, in RFC 822 date format

In your code you can always test to see if an element is non-empty with:

1
2
3
if ($item->getPropname()) {
    // ... proceed.
}

Where relevant, Zend\Feed supports a number of common RSS extensions including Dublin Core, Atom (inside RSS) and the Content, Slash, Syndication, Syndication/Thread and several other extensions or modules.

For further information, the official RSS 2.0 specification is available at: http://blogs.law.harvard.edu/tech/rss

Table Of Contents

Previous topic

Retrieving Feeds from Web Pages

Next topic

Consuming an Atom Feed

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Consuming an RSS Feed on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.