Using Item Elements

Channel element information is only half of what Zend_Feed can parse. The second main section of an RSS document are the items (syndicated articles) contained in the document.

Table 7-18 shows the full list of tags the Zend_Feed component supports when parsing the document's data. Using the table, you'll expand on the section's example and parse the two articles contained in the rssexample.xml RSS feed.

Table 7-18. Zend_Feed_RSS Supported Item Tags

Element Method Call Return Type author author() String title title() String link link() String description description() String

guid

guid()

String

comments

comments()

String

pubDate

pubDate()

String

source

source()

Array; elements: {title, url}

category

category()

M-Array; elements: {term, scheme}

enclosure

enclosure()

M-Array; elements: {url, type, length}

Updating the rssTestAction(), you create the code shown in Listing 7-36.

Updating the rssTestAction(), you create the code shown in Listing 7-36.

Listing 7-36. Working with Item Information <?php public function rsstestAction() {

//Load the RSS document. try{

SrssFeedAsFile = '<PATH TO rssexample.xml FILE>'; $feed = Zend_Feed::importFile($rssFeedAsFile);

//Parse and store the RSS data. $this->view->title = $feed->title(); $this->view->link = $feed->link(); $this->view->description = $feed->description(); $this->view->ttl = $feed->ttl(); $this->view->copyright = $feed->copyright(); $this->view->language = $feed->language(); $this->view->category = $feed->category(); $this->view->pubDate = $feed->pubDate();

//Get the articles $articles = array(); foreach($feed as $article){ $articles[] = array( "title" => $article->title(), "description" => $article->description(), "link" => $article->link(),

"author" => $article->author(), "enclosure" => array("url" => $article->enclosure['url'], "type" => $article->enclosure['type']));

$this->view->articles = $articles; }catch(Zend_Feed_Exception $e){ throw $e; }

Before moving on to the updated view of the action, let's look at how you fetch the article information stored in the RSS feed and the Zend_Feed_RSS object.

After loading the RSS feed using the importFile() method, you don't have to do additional coding to parse the data. Zend_Feed automatically parses the channel information and the item information. The feed data is stored in a Zend_Feed_RSS object and is returned upon a successful load of the RSS.

The Zend_Feed_RSS object is a type of Zend_Feed_Abstract object. Why is this important? Because the Zend_Feed_Abstract object implements an iterator and it allows you to use the returned object, Zend_Feed_RSS, inside an iteration loop such as foreach to access the items set for syndication. When using the iterator functionality of the Zend_Feed_RSS object, you loop through the items contained in the feed. By looping through the items, you access each article individually. In the example, you loop through the $feed object and call the getter methods for the title, link, description, and author to fetch the String values stored inside these elements. When you reach an element that contains attributes, the getter method returns an array. The array contains key-value pairs, and the value can be returned by using the following format:

$object-><elementName>['attributeName']

In the example, you accessed the value stored inside the url attribute by directly calling the url key contained in the enclosure element:

$article->enclosure['url'];

Finally, you place all the items into an array you can later use in the view shown in Listing 7-37.

Listing 7-37. rss-test.phtml

<?php echo $this->doctype('XHTML1_STRICT'); ?>

<html xmlss="http://www.w3.org/1999/xhtml" xml:lang= "en" lang="en">

<?php echo $this->headTitle('LoudBite.com - Artist List'); ?> </head> <body>

<?php echo $this->render("includes/header.phtml")?> <h3>Channel Information</h3>

Title: <a href="<?php echo $this->link ?>"><?php echo $this->title ?></a>

- <i><?php echo $this->copyright ?></i><br>

Description: <?php echo $this->description ?><br>

Category: <?php echo $this->category ?><br>

Published Date: <?php echo $this->pubDate ?><br>

<?php foreach($this->articles as $article){ ?>

<a href="<?php echo $article['link'] ?>"><?php echo $article['title'] ?></a> </b><br>

<?php echo $article['description'] ?><br/> <?php } ?> </p>

Now focus your attention on creating an RSS feed of the site using Zend.

0 0

Post a comment

  • Receive news updates via email from this site