PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Importing Feeds

Importing a feed with Zend_Feed_Reader is not that much different to Zend_Feed. Feeds can be imported from a string, file, URI or an instance of type Zend_Feed_Abstract. Importing from a URI can additionally utilise a HTTP Conditional GET request. If importing fails, an exception will be raised. The end result will be an object of type Zend_Feed_Reader_FeedInterface, the core implementations of which are Zend_Feed_Reader_Feed_Rss and Zend_Feed_Reader_Feed_Atom (Zend_Feed took all the short names!). Both objects support multiple (all existing) versions of these broad feed types.

In the following example, we import an RDF/RSS 1.0 feed and extract some basic information that can be saved to a database or elsewhere.

<?php
$feed 
Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
$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(),
        
'authors'       => $entry->getAuthors(),
        
'link'         => $entry->getLink(),
        
'content'      => $entry->getContent()
    );
    
$data['entries'][] = $edata;
}

The example above demonstrates Zend_Feed_Reader's API, and it also demonstrates some of its internal operation. In reality, the RDF feed selected does not have any native date or author elements, however it does utilise the Dublin Core 1.1 module which offers namespaced creator and date elements. Zend_Feed_Reader falls back on these and similar options if no relevant native elements exist. If it absolutely cannot find an alternative it will return NULL, indicating the information could not be found in the feed. You should note that classes implementing Zend_Feed_Reader_FeedInterface also implement the SPL Iterator and Countable interfaces.

Feeds can also be imported from strings, files, and even objects of type Zend_Feed_Abstract.

<?php
// from a URI
$feed Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');

// from a String
$feed Zend_Feed_Reader::importString($feedXmlString);

// from a file
$feed Zend_Feed_Reader::importFile('./feed.xml');

// from a Zend_Feed_Abstract object
$zfeed Zend_Feed::import('http://www.planet-php.net/atom/');
$feed  Zend_Feed_Reader::importFeed($zfeed);

Zend Framework