PHP RSS/Atom Reader Library menggunakan Zend Feed Reader

Kalau kamu sedang atau akan membuat aplikasi web yang dapat mengambil dan membaca feed seperti RSS atau Atom dari suatu situs, Zend Feed Reader bawaan Zend Framework bisa kamu jadikan pilihan. Versi yang saya gunakan di sini Zend Framework 1.9.5. Bila kamu hanya perlu library feed readernya saja, berikut library Zend Feed Reader yang sudah saya pisahkan dari Zend Framework. download di sini.

API yang disediakan oleh library ini sangat memudahkan kita untuk mendapatkan data atau content yang ada di feed. Versi feed yang didukung yaitu RDF/RSS 1.0, RSS 2.0 and Atom 0.3/1.0.
Cukup dengan memberikan URI feed tersebut berada, library yang mengurus sisanya. Berikut cuplikan kodenya:

$feed = Zend_Feed_Reader::import('http://www.syabac.web.id/feed/rss');
$data = array(
    'title'        => $feed->getTitle(),
    'link'         => $feed->getLink(),
    'dateModified' => $feed->getDateModified(),
    'description'  => $feed->getDescription(),
    'language'     => $feed->getLanguage(),
    'entries'      => array(),
);
 
foreach ($feed as $entry) {
    $edata = array(
        'title'        => $entry->getTitle(),
        'description'  => $entry->getDescription(),
        'dateModified' => $entry->getDateModified(),
        'author'       => $entry->getAuthor(),
        'link'         => $entry->getLink(),
        'content'      => $entry->getContent()
    );
    $data['entries'][] = $edata;
}

Selain mengimport dari URI, kita juga bisa mengimport dari string XML atau local file. Berikut kodenya:

// from a URI
$feed = Zend_Feed_Reader::import('http://www.syabac.web.id/feed/');
 
// from a String
$feed = Zend_Feed_Reader::importString($feedXmlString);
 
// from a file
$feed = Zend_Feed_Reader::importFile('./feed.xml');

Selain untuk membaca data dari feed, kita juga bisa memanipulasi dan menyimpan feed yang sudah kita import tadi.

Finding and Locating Feed URIs from website

Selain fitur-fitur yang sudah saya tulis di atas, Zend Feed Reader juga menyediakan API yang berfungsi mencari atau mendapatkan feed URI suatu situs. Berikut contohnya:

$links = Zend_Feed_Reader::findFeedLinks('http://www.syabac.web.id');
if(isset($links->rdf)) {
    echo $links->rdf, "\n"; // http://www.syabac.web.id/feed/rdf/
}
if(isset($links->rss)) {
    echo $links->rss, "\n"; // http://www.syabac.web.id/feed/rss/
}
if(isset($links->atom)) {
    echo $links->atom, "\n"; // http://www.syabac.web.id/feed/atom/
}
//mencetak semua feed URI yang ditemukan
foreach ($links as $link) {
    echo $link['href'], "\n";
}

Berikut beberapa API yang disediakan Zend Feed Reader (diambil dari Zend Framework Manual).

Feed Level API Methods

getId() Returns a unique ID associated with this feed
getTitle() Returns the title of the feed
getDescription() Returns the text description of the feed
getLink() Returns a URI to the HTML website
containing the same or
similar information as this feed (i.e. if the feed is from a blog,
it should provide the blog’s URI where the
HTML version of the entries can be read).
getFeedLink() Returns the URI of this feed, which should be the
same as the URI used to import the feed
getAuthors() Returns an array of all authors associated with this feed
including email address in the author string if available
getAuthor(integer $index = 0) Returns either the first author known, or with the
optional $index parameter any specific
index on the array of Authors (returning null if an
invalid index).
getDateCreated() Returns the date on which this feed was created. Generally
only applicable to Atom where it represents the date the resource
described by an Atom 1.0 document was created.
getDateModified() Returns the date on which this feed was last modified
getLanguage() Returns the language of the feed (if defined) or simply the
language noted in the XML document
getGenerator() Returns the generator of the feed, e.g. the software which
generated it. This may differ between RSS and Atom
since Atom defines a different notation.
getCopyright() Returns any copyright notice associated with the feed
getHubs() Returns an array of all Hub Server URI endpoints which
are advertised by the feed for using with the Pubsubhubbub
Protocol, allowing subscriptions to the feed for real-time updates.
getDomDocument() Returns the parent
DOMDocument object for the
entire source XML document
getElement() Returns the current feed level
DOMElement object
saveXml() Returns a string containing an XML document of the
entire feed element (this is not the original
document but a rebuilt version)
getXpath() Returns the DOMXPath object
used internally to run queries on the
DOMDocument object (this
includes core and Extension namespaces
pre-registered)
getXpathPrefix() Returns the valid DOM path prefix prepended
to all XPath queries matching the feed being queried
getEncoding() Returns the encoding of the source XML document
(note: this cannot account for errors such as the
server sending documents in a different encoding)
count() Returns a count of the entries or items this feed contains
(implements SPL Countable
interface)
current() Returns either the current entry (using the current index
from key())
key() Returns the current entry index
next() Increments the entry index value by one
rewind() Resets the entry index to 0
valid() Checks that the current entry index is valid, i.e.
it does fall below 0 and does not exceed the number
of entries existing.
getExtensions() Returns an array of all Extension objects loaded for
the current feed (note: both feed-level and entry-level Extensions
exist, and only feed-level Extensions are returned here).
The array keys are of the form {ExtensionName}_Feed.
getExtension(string $name) Returns an Extension object for the feed registered under the
provided name. This allows more fine-grained access to
Extensions which may otherwise be hidden within the implementation
of the standard API methods.
getType() Returns a static class constant (e.g.
Zend_Feed_Reader::TYPE_ATOM_03,
i.e. Atom 0.3) indicating exactly what kind of feed
is being consumed.

Entry Level API Methods

getId() Returns a unique ID for the current entry
getTitle() Returns the title of the current entry
getDescription() Returns a description of the current entry
getLink() Returns a URI to the HTML version
of the current entry
getPermaLink() Returns the permanent link to the current entry
getAuthors() Returns an array of all authors associated with this entry
including email address in the author string if available
getAuthor($index = 0) Returns either the first author known, or with the
optional $index parameter any specific
index on the array of Authors (returning null if an
invalid index).
getDateCreated() Returns the date on which the current entry was
created. Generally only applicable to Atom where it
represents the date the resource described by an
Atom 1.0 document was created.
getDateModified() Returns the date on which the current entry was last
modified
getContent() Returns the content of the current entry (this has any
entities reversed if possible assuming the content type is
HTML). The description is returned if a
separate content element does not exist.
getEnclosure() Returns an array containing the value of all
attributes from a multi-media element including
as array keys: url,
length, type.
getCommentCount() Returns the number of comments made on this entry at the
time the feed was last generated
getCommentLink() Returns a URI pointing to the HTML
page where comments can be made on this entry
getCommentFeedLink(string $type =
'atom'|'rss')
Returns a URI pointing to a feed of the provided type
containing all comments for this entry (type defaults to
Atom/RSS depending on current feed type).

Informasi lebih lanjut dan lengkap ada di Zend Framework Manual.

Referensi: Zend Framework 1.9.5 Manual