Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

weather.inc.php

Go to the documentation of this file.
00001 <?
00002 /*************************************************
00003 *
00004 *   Geocode PHP Class - Access Yahoo API.
00005 *   Copyright 2006 Zach Smith
00006 *   http://hoeken.ubersonic.org
00007 *   License: GPL
00008 *
00009 *************************************************/
00010 
00011 class YahooWeather extends REST
00012 {
00013     public function __construct()
00014     {
00015         //heres our base.
00016         $base = 'http://xml.weather.yahoo.com/forecastrss';
00017     
00018         //set up our rest action
00019         parent::__construct($base);
00020     }
00021 
00022     public function search($location, $units = 'f', $parse = true)
00023     {
00024         $data = $this->request(array(
00025             'p' => $location,
00026             'u' => $units
00027         ));
00028 
00029         //do we parse it?
00030         if ($parse)
00031             return $this->parseData($data);
00032         else
00033             return $data;
00034     }
00035 
00036     protected function parseData($data)
00037     {
00038         //inits!
00039         $rval = array();
00040 
00041         //load our xml.
00042         $xml = simplexml_load_string($data);
00043     
00044         //get our html..
00045         $rval['html'] = (string)$xml->channel->item->description;
00046         $rval['link'] = (string)$xml->channel->link;
00047     
00048         //yahoo namespaces... barf.
00049         foreach ($xml->channel->children('http://xml.weather.yahoo.com/ns/rss/1.0') AS $entry)
00050         {
00051             //location?
00052             if ($entry->getName() == 'location')
00053             {
00054                 foreach ($entry->attributes() AS $att)
00055                 {
00056                     if ($att->getName() == 'city')
00057                         $rval['city'] = (string)$att;
00058                     else if ($att->getName() == 'region')
00059                         $rval['state'] = (string)$att;
00060                 }
00061             }
00062         }
00063         
00064         //now we want to get current conditions
00065         $day = 'today';
00066         foreach ($xml->channel->item->children('http://xml.weather.yahoo.com/ns/rss/1.0') AS $entry)
00067         {
00068             //parse out each of our days.
00069             if ($entry->getName() == 'condition')
00070                 $rval['now'] = $this->parseDay($entry);
00071             else if ($entry->getName() == 'forecast' && $day == 'today')
00072             {
00073                 $rval['today'] = $this->parseDay($entry);
00074                 $day = 'tomorrow';
00075             }
00076             else if ($entry->getName() == 'forecast')
00077                 $rval['tomorrow'] = $this->parseDay($entry);
00078         }
00079 
00080         return $rval;
00081     }
00082 
00083     protected function parseDay($day)
00084     {
00085         $rval = array();
00086 
00087         //go thru the attributes and get the day.
00088         foreach ($day->attributes() AS $att)
00089         {
00090             if ($att->getName() == 'code')
00091             {
00092                 $rval['code'] = (string)$att;
00093                 $rval['image'] = "http://us.i1.yimg.com/us.yimg.com/i/us/we/52/$rval[code].gif";
00094             }
00095             else if ($att->getName() == 'text')
00096                 $rval['condition'] = (string)$att;
00097             else if ($att->getName() == 'day')
00098                 $rval['day'] = (string)$att;
00099             else if ($att->getName() == 'temp')
00100                 $rval['temp'] = (string)$att;
00101             else if ($att->getName() == 'high')
00102                 $rval['high'] = (string)$att;
00103             else if ($att->getName() == 'low')
00104                 $rval['low'] = (string)$att;
00105         }
00106 
00107         if ($day->getName() == 'condition')
00108             $rval['day'] = 'Now';
00109 
00110         return $rval;
00111     }
00112 }
00113 ?>

Generated on Fri Oct 27 12:26:40 2006 for BaseJumper by doxygen 1.3.9.1