00001 <?
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 class YahooWeather extends REST
00012 {
00013 public function __construct()
00014 {
00015
00016 $base = 'http:
00017
00018
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
00030 if ($parse)
00031 return $this->parseData($data);
00032 else
00033 return $data;
00034 }
00035
00036 protected function parseData($data)
00037 {
00038
00039 $rval = array();
00040
00041
00042 $xml = simplexml_load_string($data);
00043
00044
00045 $rval['html'] = (string)$xml->channel->item->description;
00046 $rval['link'] = (string)$xml->channel->link;
00047
00048
00049 foreach ($xml->channel->children('http:
00050 {
00051
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
00065 $day = 'today';
00066 foreach ($xml->channel->item->children('http:
00067 {
00068
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
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 ?>