Introduction
Reading RSS / Atom feeds
Fegg\Tool\RSS loads an RSS 1.0 (RDF), RSS 2.0 or Atom feed and returns its posts as arrays. A feed URL is fetched with Fegg\Http\Client (10 second timeout by default); a local file can be read as well.
How to use
Import Fegg\Tool\RSS and create it with new, passing the feed URL. The constructor throws a \RuntimeException when the feed cannot be fetched or read, or is not RSS or Atom, so catch it when the feed may be unavailable.
Controller
<?php
namespace Site\Controller;
defined('FEGG_CORE_DIR') || exit;
use Fegg\Tool\RSS;
class Index extends Controller
{
public function index()
{
try {
$rss = new RSS('https://example.com/feed');
$this->page['posts'] = $rss->fetchAll(10);
} catch (\RuntimeException $e) {
$this->page['posts'] = array();
}
$this->displayPage('index');
}
}
Entry data
Each entry returned by RSS::fetch and RSS::fetchAll is an array with these keys. A field the feed does not have is '' (an empty array for category and tag).
| Key | Data type | Remarks |
|---|---|---|
| title | string | Title |
| pubDate | string | Date as written in the feed (RSS 2.0: pubDate, or dc:date; RSS 1.0: dc:date; Atom: published, or updated) |
| datetime | DateTime/null | pubDate as a DateTime object, null when there is no date or it cannot be read |
| link | string | Link URL (Atom: the href of <link rel="alternate">) |
| description | string | Description (RSS: HTML entities decoded; Atom: summary) |
| content | string | content:encoded (HTML entities decoded); Atom: content |
| author | string | dc:creator (RSS 2.0: or author; Atom: author > name) |
| category | array | Category names (RSS 2.0: category; RSS 1.0: dc:subject; Atom: the term of category) |
| tag | array | Always an empty array (not implemented) |
Methods
RSS::__construct
Load a feed
RSS::fetch
Get the next entry of the feed
RSS::fetchAll
Get the remaining entries of the feed