Difference between revisions of "Workshop - week 06"

From mi-linux
Jump to navigationJump to search
Line 161: Line 161:
 
== More on web feeds with Zend ==
 
== More on web feeds with Zend ==
 
* http://framework.zend.com/manual/en/zend.feed.html
 
* http://framework.zend.com/manual/en/zend.feed.html
 
You can also use the yahoo weather service as an Rss feed to show the people the weather around the weather.
 
 
1) You would firstly need to make a controller just like at the top of this wiki.
 
 
2) You would then need to implement the code in the view controller.
 
 
This would be the code which would be used to implement the weather in wolverhampton into the webpage: http://weather.yahooapis.com/forecastrss?p=UKXX0159&u=f
 
 
There is also an XML file which you could be used to implement into the webpage.
 
 
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
 
<rss version="2.0" xmlns:yweather="http://weather.yahooapis.com/ns/rss/1.0"
 
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
 
<channel>
 
  <title>Yahoo! Weather - Sunnyvale, CA</title>
 
  <link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/
 
  *http://weather.yahoo.com/forecast/94089_f.html</link>
 
  <description>Yahoo! Weather for Sunnyvale, CA</description>
 
  <language>en-us</language>
 
  <lastBuildDate>Tue, 29 Nov 2005 3:56 pm PST</lastBuildDate>
 
  <ttl>60</ttl>
 
  <yweather:location city="Sunnyvale" region="CA" country="US"></yweather:location>
 
  <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"></yweather:units>
 
  <yweather:wind chill="57" direction="350" speed="7"></yweather:wind>
 
  <yweather:atmosphere humidity="93" visibility="1609" pressure="30.12" rising="0"></yweather:atmosphere>
 
  <yweather:astronomy sunrise="7:02 am" sunset="4:51 pm"></yweather:astronomy>
 
  <image>
 
      <title>Yahoo! Weather</title>
 
      <width>142</width>
 
      <height>18</height>
 
      <link>http://weather.yahoo.com/</link>
 
      <url>http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>
 
  </image>
 
  <item>
 
      <title>Conditions for Sunnyvale, CA at 3:56 pm PST</title>
 
      <geo:lat>37.39</geo:lat>
 
      <geo:long>-122.03</geo:long>
 
      <link>http://us.rd.yahoo.com/dailynews/rss/weather/
 
      Sunnyvale__CA/*
 
      http://weather.yahoo.com/ forecast/94089_f.html
 
      </link>
 
      <pubDate>Tue, 29 Nov 2005 3:56 pm PST</pubDate>
 
      <yweather:condition text="Mostly Cloudy" code="26" temp="57" date="Tue, 29 Nov 2005 3:56
 
          pm PST"></yweather:condition>
 
      <description><![CDATA[
 
<img src="http://l.yimg.com/a/i/us/we/52/26.gif" /><br />
 
<b>Current Conditions:</b><br />
 
Mostly Cloudy, 57 F<p />
 
<b>Forecast:</b><BR />
 
  Tue - Mostly Cloudy. High: 62 Low: 45<br />
 
  Wed - Mostly Cloudy. High: 60 Low: 52<br />
 
  Thu - Rain. High: 61 Low: 46<br />
 
<br />
 
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/94089_f.html">Full Forecast at Yahoo! Weather</a><BR/>
 
(provided by The Weather Channel)<br/>]]>
 
      </description>
 
      <yweather:forecast day="Tue" date="29 Nov 2005" low="45" high="62" text="Mostly Cloudy"
 
          code="27"></yweather:forecast>
 
      <yweather:forecast day="Wed" date="30 Nov 2005" low="52" high="60" text="Mostly Cloudy"
 
          code="28"></yweather:forecast>
 
      <guid isPermaLink="false">94089_2005_11_29_15_56_PST</guid>
 
  </item>
 
</channel>
 
</rss>
 
 
There might be some things which are not correct, feel free to edit.
 
 
By Amar Joshi (0709436).
 

Revision as of 00:53, 3 April 2009

Main Page >> Web Frameworks >> Web Frameworks - Workbook >> Workshop - week 06

Consuming an existing web feed

First you need to create a new controller (new file!), with just one default "index" action.

In this action you simply create your feed object by calling the Zend_Feed::import function, which take as a parameter the URL of the web feed (the BBC news feed in this example).

You then copy the content of the feed to an array, and pass the array to the view.

<?php

// application/controllers/FeedController.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'];
    }

}

You also need to create a view (new file!) for your new controller/action. The view simply loops through and displays the array provided by the controller:


<!-- application/views/scripts/feed/index.phtml -->

Feed Entries: <br />
<dl>
    <!-- Loop through the entries provided by the controller -->
    <? foreach ($this->entries as $entry): ?>
    <dt><?= $this->escape($entry['title']) ?></dt>
    <dd><?= $this->escape($entry['description']) ?></dd>
    <? endforeach ?>
</dl>

You can then simply access your consumed feed at the following URL: http://mi-linux.wlv.ac.uk/~0123456/zend/public/feed/ (please replace 0123456 with your own student number!)

Added By Daniel Hardy 0607197

The above url should be ( or at least was for me!) http://mi-linux.wlv.ac.uk/~0123456/QuickStart/public/feed

What you should see on your screen:

Feed 2.gif

Note: For simplicity and clarity we are importing the feed in the controller, but a feed is an XML file, and as such could (should?) be considered as a data source. In line with MVC principles it would therefore be better practice to move the feed import code to a "model" file.

Producing your own web feed

Note: You don't want any layouts for this action, as you are displaying pure XML, not an HTML page. One way of achieving this is to change the current layout to very simple layout located in your layout folder (and called "blank.phtml" in this example).

(application/layouts/scripts/blank.phtml)

<?= $this->layout()->content ?>

Let's create a new "feed" action in your existing "Guestbook" controller. (see Workshop - week 04 - Models and databases)

The first thing you need to do is to specify that you want to use you "blank" layout for this action.

You then transfer your data from the model to an array, and convert the array to a feed using the Zend_Feed::importArray function.

Easy!


    // application/controllers/GuestbookController.php

    public function feedAction()
    {
      // Change layout to blank.phtml (empty file)
      $this->_helper->layout->setLayout('blank');

      // Set feed values
      $array['title'] = 'Guestbook feed';
      $array['link'] = 'http://www.somewhere.com/';
      $array['description'] = 'This is guestbook feed';
      $array['charset'] = 'utf8';
      // You need to change this line to read $array['charset'] = 'utf-8'; (Note the change to the 'utf8' part)
      
      // Loop through db records and add feed items to array
      $model = $this->_getModel();
      foreach($model->fetchEntries() as $one_item)
      {
        $array['entries'][] = array(
          'title' => $one_item['email'],
          'link' => 'http://www.somewhere.com/123',
          'description' => $one_item['comment']
        );
      }
            
      // Import rss feed from array
      $feed = Zend_Feed::importArray($array, 'rss');

      // Send http headers and dump the feed
      $feed->send();
    }

And of course we need a new view for our new action. It is rather simple, as all it only has to display the feed:

(application/views/scripts/guestbook/feed.phtml)

<?
  echo $this->feedxml;

To test your new "feed" action simply visit the following URL: http://mi-linux.wlv.ac.uk/~0123456/zend/public/guestbook/feed (please replace 0123456 with your own student number!)

Added By Sanjeev Summan 0718721

The above url should be ( or at least was for me!) http://mi-linux.wlv.ac.uk/~0123456/QuickStart/public/guestbook/feed

What you should see on your screen (if you are using Firefox 3 anyway... not sure what feeds look like in other browsers):

Feed.gif

More on web feeds with Zend