Forecast values from Weather Underground in PHP
Posted: Sunday 20 March 2016 11:12
Hi all
I saw, if I am not wrong, that in the Weather Underground dummy devices there isn't any information regarding forecast for next days.
Starting from the php found in the wiki for Automatic Weather forecast tweet (https://www.domoticz.com/wiki/Automatic ... P_version) I've made a php that will collect the forecast data using the "forecast" data features and getting the name of the day of the forecast, the forecast type (sunny, partlycloudy,...) and also the icon that WU use for that forecast type.
Of course, the data grabbed can be more than mine, accordingly to what's inside the data from WU.
What I would like to show in my frontpage is three icons with the forecast for the next three days.
The code is here:
Starting from a frontpage made with html and json, also this grabbed from a thread in this forum from Gerard33 (thanks!!!), how may I call the php and get the values in the variables from php to json to evaluate with if clause?
I'm not very good in coding, just some, and I am not aware of how to do it.
Can you help on this?
Thanks
Woody
I saw, if I am not wrong, that in the Weather Underground dummy devices there isn't any information regarding forecast for next days.
Starting from the php found in the wiki for Automatic Weather forecast tweet (https://www.domoticz.com/wiki/Automatic ... P_version) I've made a php that will collect the forecast data using the "forecast" data features and getting the name of the day of the forecast, the forecast type (sunny, partlycloudy,...) and also the icon that WU use for that forecast type.
Of course, the data grabbed can be more than mine, accordingly to what's inside the data from WU.
What I would like to show in my frontpage is three icons with the forecast for the next three days.
The code is here:
Code: Select all
#!/usr/bin/php
<?php
//---get forecast for the next three days -------------------------
//---see the wunderground webpage for other weather information download
//---uselang:xx for language of your country
//---change hourly to other values for other weather information
//---change country and city after q
$json_string = file_get_contents("http://api.wunderground.com/api/xxxxxxxxxx/forecast/lang:EN/q/locid:xxxxxxxx;loctype:1.json");
$parsed_json = json_decode($json_string, true);
//---for debugging and development of own forecast combination
//---this file will show the complete array of a weather forecast
//$wetterdatei = "/home/pi/domoticz/scripts/wetter_json.txt";
//$wd = fopen ($wetterdatei, "w+");
//fwrite ($wd, print_R($parsed_json, TRUE));
//fclose ($wd);
//--------- Weather in xh ------------------------------------
//---look at the wetter_json.txt file about the structure
$parsed_json = $parsed_json['forecast']['txt_forecast'];
//print_r($parsed_json);
$date_forecast = $parsed_json['date'];
echo $date_forecast."\n";
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['forecast']['txt_forecast']['forecastday'][0];
$icon1 = $parsed_json['icon'];
$icon_url1 = $parsed_json['icon_url'];
$day1 = $parsed_json['title'];
echo $icon1.":".$day1."\n";
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['forecast']['txt_forecast']['forecastday'][2];
$icon2 = $parsed_json['icon'];
$icon_url2 = $parsed_json['icon_url'];
$day2 = $parsed_json['title'];
echo $icon2.":".$day2."\n";
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['forecast']['txt_forecast']['forecastday'][4];
$icon3 = $parsed_json['icon'];
$icon_url3 = $parsed_json['icon_url'];
$day3 = $parsed_json['title'];
echo $icon3.":".$day3."\n";
?>I'm not very good in coding, just some, and I am not aware of how to do it.
Can you help on this?
Thanks
Woody

