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.de/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! 🙂
[cc lang=”php” escaped=”true” width=”600″]
{‘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 “
$title
“.$day[‘forecast_weather’].”
“.$day[‘forecast_high’].”
“.$day[‘forecast_low’].”
“.$day[‘forecast_rainrisk’].”
Wind aus
“.$day[‘forecast_winddirection’].”
mit
“.$day[‘forecast_wind’].”
“;
}
$Today = printDay(getDay(0),”Vorhersage Heute:”);
$Tomorrow = printDay(getDay(1),”Vorhersage Morgen:”);
$DayAfterTomorrow = printDay(getDay(2),”Vorhersage Übermorgen:”);
echo ‘
‘;echo “
$location
“;
echo “
Aktuell: $weather
Temperatur: $temp_cn °C
Wind aus: $wind_directionn mit “.$wind_speed.”
“;
echo “
$Today
“;
echo “
$Tomorrow
“;
echo “
$DayAfterTomorrow
“;
echo ““;
?>
[/cc]
liked this article?
- only together we can create a truly free world
- plz support dwaves to keep it up & running!
- (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
- really really hate advertisement
- contribute: whenever a solution was found, blog about it for others to find!
- talk about, recommend & link to this blog and articles
- thanks to all who contribute!