Hi Innyinskip
Code isn't very elegant and I'm sure it could be done better.
For the HTML, I use a couple of Divs
\themes\<yourthemename>\templates\Includes\sidebar.ss
<div id="YahooWeather">
<% control YahooWeather %>
<div id="YahooWeatherHeader">
<h3 class="smallCaps">$City Weather</h3>
</div>
<div id="YahooWeatherContent" style="background: $bgColor url('$ImageURL') no-repeat top left; height: 180px; border-bottom: 1px dashed #060; border-top: 1px dashed #060;" >
<p class="YahooTempText">$Temp°$TempUnit</p>
<p class="YahooConditionText">$ConditionText</p>
</div><!-- YahooWeatherContent -->
<% end_control %>
</div><!-- YahooWeather -->
For the code that controls the %YahooWeather% widget
\mysite\code\Page.php
Go to the Yahoo weather site and find the ID for the city you want to show.
I've loaded two cities, Dunoon and Edinburgh into an array $LocationID
The function currently uses the Dunoon ID in the $params variable.
So if you wanted, say Glasgow, find the Glasgow ID from the Yahoo Weather site, say its's UK666, your code would be
$LocationID = array("Glasgow" => "UK666","Dunoon" => "UKXX0894","Edinburgh" => "UKXX0052");
$params = array('p' => $LocationID["Glasgow"],'u'=>'c');
function YahooWeather(){
try{
/*$xml = geturl("http://xml.weather.yahoo.com/forecastrss?p=UKXX0052");*/
$cache_expiry = 60*60;
$xml = new RestfulService("http://xml.weather.yahoo.com/forecastrss", $cache_expiry );
$LocationID = array("Dunoon" => "UKXX0894","Edinburgh" => "UKXX0052");
$params = array('p' => $LocationID["Dunoon"],'u'=>'c');
$xml->setQueryString($params);
$conn = $xml->connect();
$msgs = $xml->getValues($conn, "channel");
$conditions = $xml->searchAttributes($conn, "//yweather:condition");
$winds = $xml->searchAttributes($conn, "//yweather:wind");
$locations = $xml->searchAttributes($conn, "//yweather:location");
$atmospheres = $xml->searchAttributes($conn, "//yweather:atmosphere");
$astros = $xml->searchAttributes($conn, "//yweather:astronomy");
$units = $xml->searchAttributes($conn, "//yweather:units");
$compassPoints = array(
1 => "N",
2 => "NbE",
3 => "NNE",
4 => "NEbN",
5 => "NE",
6 => "NEbE",
7 => "ENE",
8 => "EbN",
9 => "E",
10 => "EbS",
11 => "ESE",
12 => "SEbE",
13 => "SE",
14 => "SEbS",
15 => "SEbS",
16 => "SbE",
17 => "S",
18 => "SbW",
19 => "SSW",
20 => "SWbS",
21 => "SW",
22 => "SWbW",
23 => "WSW",
24 => "WbS",
25 => "W",
26 => "WbN",
27 => "WNW",
28 => "NWbW",
29 => "NW",
20 => "NWbN",
41 => "NNW",
32 => "NbW"
);
$BarometerState = array(
0 => "steady",
1 => "rising",
2 => "falling"
);
/* Get base info */
$output = new DataObjectSet();
foreach($msgs as $msg){
$resArray = array(
"Description" => $msg->description,
"LastBuildDate" => DBField::Create("SSDatetime", $msg->lastBuildDate)->ago()
);
}
/* Get condition info */
foreach($conditions as $condition){
$resArray += array(
"ConditionText" => $condition->text,
"ConditionCode" => ($condition->code==3200)?44:$condition->code,
"Temp" => $condition->temp,
);
}
/* Get Units info */
foreach($units as $unit){
$resArray += array(
"TempUnit" => $unit->temperature,
"DistanceUnit" => $unit->distance,
"PressureUnit" => $unit->pressure,
"WindSpeedUnit" => $unit->speed
);
}
/* Get Location info */
foreach($locations as $location){
$resArray += array(
"City" => $location->city,
"Region" => $location->region,
"Country" => $location->country
);
}
/* Get Astronomy info */
foreach($astros as $astro){
$resArray += array(
"Sunrise" => $astro->sunrise,
"Sunset" => $astro->sunset
);
}
/* Get Atmosphere info */
foreach($atmospheres as $atmosphere){
$resArray += array(
"Humidity" => $atmosphere->humidity,
"HumidityText" => $atmosphere->humidity."%",
"Visibility" => round($atmosphere->visibility),
"VisibilityText" => round($atmosphere->visibility).$resArray["DistanceUnit"],
"Pressure" => round($atmosphere->pressure),
"PressureText" => round($atmosphere->pressure).$resArray["PressureUnit"],
"Rising" => $atmosphere->rising,
"BarometerState" => $BarometerState[$atmosphere->rising]
);
}
/* Get Wind info */
foreach($winds as $wind){
$resArray += array(
"WindChill" => $wind->chill,
"WindDirection" => $compassPoints[intval((($wind->direction/360)/32)+0.5)+1],
"WindSpeed" => intval($wind->speed),
"WindSpeedText" => intval($wind->speed).$resArray["WindSpeedUnit"]
);
}
/* find out if it is day or night - use sunset and sunrise times */
date_default_timezone_set('DST');
$timeNow = strtotime(date("g:i a"));
$timeSet = strtotime($resArray["Sunset"]);
$timeRise = strtotime($resArray["Sunrise"]);
if (($timeNow>=$timeRise) && ($timeNow<$timeSet)){
$isDayOrNight = "d";
$bgColor ="#069";
} else {
$isDayOrNight = "n";
$bgColor="#000";
}
$resArray += array("Today" => $isDayOrNight, "LocalTime" => date("g:ia"),"bgColor" => $bgColor);
/* adjust image url output to include day or night pics */
$ImageURL = "/images/weather/".$resArray["ConditionCode"].$isDayOrNight.".png";
$ImageURLSmall = "http://l.yimg.com/a/i/us/nws/weather/gr/".$resArray["ConditionCode"]."s.png";
$resArray += array("ImageURL" => $ImageURL, "ImageURLSmall" => $ImageURLSmall);
/* Output Results */
$output->push(new ArrayData($resArray));
return $output;
} catch(Exception $e) {
return false;
}
}
For the images, unzip the attachment into the root /images folder.
So your weather images should be in e.g. /images/weather/11d.png
The images generally are numeric and end with d for day, n for night.
If you have any other Q's then send me an email info@gravitationalfx.com. I'll pick it up there
Cheerz,
Wil.