unfortunately, it’s not so easy to get a weather forecast widget running for your website… because (e.g. google, yahoo and co) they want to make money with providing this services… so they limit your or charge you.
WUNDERGROUND WEATHER API Example.
1. you need to login/register @ http://www.wunderground.com/
2. you need a api key
3. you need to find the location you are looking for with this: query
http://autocomplete.wunderground.com/aq?query=query
example:
City
country
http://autocomplete.wunderground.com/aq?query=Ulm&c=DE
4. construct a url that returns the info you want (see: settings)
5. in the language you want (see: language codes)
http://api.wunderground.com/api/Your_Key/forecast/lang:FR/q/France/Paris.json
example: helsinki
http://api.wunderground.com/api/YOUR-API-KEY-HERE/forecast/conditions/lang:FI/q/60.205479,24.655884.json
=
http://api.wunderground.com/api/YOUR-API-KEY-HERE/forecast/conditions/alerts/lang:FI/q/zmw:00000.11.02974.json
6. process the info with your favourite-server-side-script (see: Code Samples)
this is the source i use on my front-page:
R-Click to download the source code: https://dwaves.org/wp-content/weather.txt
(SEARCH FOR „YOUR-API-KEY-HERE“ and replace (two lines) with your Wunderground Api-Key)
this is a simplified example: (untested but should work as well
again! YOU NEED YOUR OWN API KEY FOR THIS TO WORK! 🙂
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta http-equiv="expires" content="0">
<meta http-equiv="refresh" content="3600">
<style>
.bold {
font-size:26px;
}
</style>
</head>
<?php
/* read wunderground answer to file for testing
$myFile = "json.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$json_string = file_get_contents("http://api.wunderground.com/api/YOUR-API-KEY-HERE/forecast/conditions/alerts/lang:DL/q/zmw:00000.1.10838.json");
// $json_string = file_get_contents("json.txt");
fwrite($fh, $json_string);
fclose($fh);
*/
$json_string = file_get_contents("http://api.wunderground.com/api/YOUR-API-KEY-HERE/forecast/conditions/alerts/lang:DL/q/zmw:00000.1.10838.json");
// $json_string = file_get_contents("json.txt"); // for offline testing only
$parsed_json = json_decode($json_string);
// current conditions
$location = $parsed_json->{'current_observation'}->{'display_location'}->{'full'};
$temp_c = $parsed_json->{'current_observation'}->{'temp_c'};
$relative_humidity = $parsed_json->{'current_observation'}->{'relative_humidity'};
$wind_direction = $parsed_json->{'current_observation'}->{'wind_dir'};
$weather = $parsed_json->{'current_observation'}->{'weather'};
$wind_speed = $parsed_json->{'current_observation'}->{'wind_kph'}."km/h";
// forecast
$forecast = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'};
$simpleforecast = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'};
/* DayNumber is 0 = today, 1 = tomorrow, 2 = day after tomorrow... max 6 */
function getDay($DayNumber,$forecast,$simpleforecast)
{
global $forecast, $simpleforecast;
$result = null;
$day = $forecast[$DayNumber];
$result['dayofweek'] = $day->{'title'};
$result['text'] = $day->{'fcttext_metric'};
$day_array = explode('.',$result['text']);
$result['forecast_weather'] = $simpleforecast[$DayNumber]->{'conditions'}; // "conditions": "Teils Wolkig",
$result['icon'] = $simpleforecast[$DayNumber]->{'icon_url'}; // "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
$result['forecast_high'] = "Höchsttemperatur: ".$simpleforecast[$DayNumber]->{'high'}->{'celsius'}."°C";
$result['forecast_low'] = "Tiefsttemperatur: ".$simpleforecast[$DayNumber]->{'low'}->{'celsius'}."°C";
$result['forecast_humidity'] = "Luftfeuchtigkeit: ".$simpleforecast[$DayNumber]->{'avehumidity'}."%";
$result['forecast_wind'] = "Windgeschwindigkeit: ".$simpleforecast[$DayNumber]->{'avewind'}->{'kph'}."km/h";
$result['forecast_winddirection'] = $simpleforecast[$DayNumber]->{'avewind'}->{'dir'}; // Südwest
$result['forecast_rainrisk'] = "Regenwahrscheinlichkeit: ".$simpleforecast[$DayNumber]->{'pop'}."%"; // probability of precipitation = regenwahrscheinlichkeit
return $result;
}
function printDay($day,$title)
{
return "<p>
<span class='bold'>
$title
<img src='".$day['icon']."'/> ".$day['forecast_weather']."
</span>
<br/>
".$day['forecast_high']."
<br/>
".$day['forecast_low']."
</br>
".$day['forecast_rainrisk']."
</br>
Wind aus
".$day['forecast_winddirection']."
mit
".$day['forecast_wind']."
</p>";
}
$Today = printDay(getDay(0),"Vorhersage Heute:");
$Tomorrow = printDay(getDay(1),"Vorhersage Morgen:");
$DayAfterTomorrow = printDay(getDay(2),"Vorhersage Übermorgen:");
echo '<body style="background-color: #F0F0F0;color: #333333;font-family: verdana,helvetica,arial,sans-serif;font-size: 16px;">';
echo "<h1>$location</h1>";
echo "<p>Aktuell: $weather <img src='http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif'/> <br/> Temperatur: $temp_cn °C <br/> Wind aus: $wind_directionn mit ".$wind_speed."</p>";
echo "<p>$Today</p>";
echo "<p>$Tomorrow</p>";
echo "<p>$DayAfterTomorrow</p>";
echo "</body></html>";
?>