Difference between revisions of "Workshop - week 06"
From mi-linux
Jump to navigationJump to searchLine 2: | Line 2: | ||
== Consuming an existing web feed == | == Consuming an existing web feed == | ||
+ | |||
+ | <pre> | ||
+ | <?php | ||
+ | class FeedController extends Zend_Controller_Action | ||
+ | { | ||
+ | protected $_model; | ||
+ | |||
+ | public function indexAction() | ||
+ | { | ||
+ | // Read remote feed | ||
+ | try | ||
+ | { | ||
+ | $rss = Zend_Feed::import('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'); | ||
+ | } | ||
+ | catch (Zend_Feed_Exception $e) | ||
+ | { | ||
+ | // feed import failed | ||
+ | echo "Exception caught importing feed: {$e->getMessage()}\n"; | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | // Initialize the channel data array | ||
+ | $channel = array( | ||
+ | 'title' => $rss->title(), | ||
+ | 'link' => $rss->link(), | ||
+ | 'description' => $rss->description(), | ||
+ | 'items' => array() | ||
+ | ); | ||
+ | |||
+ | // Loop over each item and store relevant data | ||
+ | foreach ($rss as $item) { | ||
+ | $channel['items'][] = array( | ||
+ | 'title' => $item->title(), | ||
+ | 'link' => $item->link(), | ||
+ | 'description' => $item->description() | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | // Pass to view | ||
+ | $this->view->entries = $channel['items']; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </pre> | ||
== Producing your own web feed == | == Producing your own web feed == |
Revision as of 13:59, 28 January 2009
Main Page >> Web Frameworks >> Web Frameworks - Workbook >> Workshop - week 06
Consuming an existing web feed
<?php class FeedController extends Zend_Controller_Action { protected $_model; public function indexAction() { // Read remote feed try { $rss = Zend_Feed::import('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'); } catch (Zend_Feed_Exception $e) { // feed import failed echo "Exception caught importing feed: {$e->getMessage()}\n"; exit; } // Initialize the channel data array $channel = array( 'title' => $rss->title(), 'link' => $rss->link(), 'description' => $rss->description(), 'items' => array() ); // Loop over each item and store relevant data foreach ($rss as $item) { $channel['items'][] = array( 'title' => $item->title(), 'link' => $item->link(), 'description' => $item->description() ); } // Pass to view $this->view->entries = $channel['items']; } }