Created
October 2, 2014 18:13
-
-
Save Hexagon/51cfb65fef696537353e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$cache_url = 'http://www.yr.no/sted/Sverige/V%C3%A4sternorrland/Johannedal/forecast.xml'; | |
$cache_file = 'cache/weather.dat'; | |
$cache_life = '120'; // Cachetid, sekunder | |
$period_text = Array('Natt','Förm.','Efterm.','Kväll');// Vädret i<period_text> | |
$sym_text = Array( | |
"1" => "Soligt och klart", | |
"2" => "Lätt molnighet", | |
"3" => "Delvis molnigt", | |
"4" => "Molnigt", | |
"5" => "Regnbyar", | |
"6" => "Regnbyar med åska", | |
"7" => "Lätt snöblandat regn", | |
"8" => "Snöbyar", | |
"9" => "Regn", | |
"10" => "Kraftigt regn", | |
"11" => "Regn med åska", | |
"12" => "Snöblandat regn", | |
"13" => "Snöfall", | |
"14" => "Snöfall med åska", | |
"15" => "Dimma", | |
"20" => "Snö, regn och åska", | |
"21" => "Snönbyar med åska", | |
"22" => "Regn med åska", | |
"23" => "Snöblandat och åska"); | |
// Cache! | |
if (!file_exists($cache_file) or (time() - filemtime($cache_file) >= $cache_life)){ | |
$live_content = file_get_contents($cache_url); | |
// Parsea! | |
$weather = new SimpleXMLElement($live_content); | |
// Skriv ut och buffra | |
ob_start(); | |
echo json_encode(Array( | |
'sunrise'=>$weather->sun['rise'], | |
'sunset'=>$weather->sun['set'], | |
'weather'=>Array( | |
Array( | |
'period'=>$period_text[intval($weather->forecast->tabular->time[0]['period'])], | |
'symbol'=>$weather->forecast->tabular->time[0]->symbol['number'], | |
'text'=>$sym_text["{$weather->forecast->tabular->time[0]->symbol['number']}"], | |
'precipitation'=>$weather->forecast->tabular->time[0]->precipitation['value'], | |
'wind'=>$weather->forecast->tabular->time[0]->windSpeed['mps'], | |
'wind_dir'=>$weather->forecast->tabular->time[0]->windDirection['code'], | |
'temperature'=>$weather->forecast->tabular->time[0]->temperature['value'], | |
'pressure'=>$weather->forecast->tabular->time[0]->pressure['value'] | |
), | |
Array( | |
'period'=>$period_text[intval($weather->forecast->tabular->time[1]['period'])], | |
'symbol'=>$weather->forecast->tabular->time[1]->symbol['number'], | |
'text'=>$sym_text["{$weather->forecast->tabular->time[1]->symbol['number']}"], | |
'precipitation'=>$weather->forecast->tabular->time[1]->precipitation['value'], | |
'wind'=>$weather->forecast->tabular->time[1]->windSpeed['mps'], | |
'wind_dir'=>$weather->forecast->tabular->time[0]->windDirection['code'], | |
'temperature'=>$weather->forecast->tabular->time[1]->temperature['value'], | |
'pressure'=>$weather->forecast->tabular->time[1]->pressure['value'] | |
), | |
Array( | |
'period'=>$period_text[intval($weather->forecast->tabular->time[2]['period'])], | |
'symbol'=>$weather->forecast->tabular->time[2]->symbol['number'], | |
'text'=>$sym_text["{$weather->forecast->tabular->time[2]->symbol['number']}"], | |
'precipitation'=>$weather->forecast->tabular->time[2]->precipitation['value'], | |
'wind'=>$weather->forecast->tabular->time[2]->windSpeed['mps'], | |
'wind_dir'=>$weather->forecast->tabular->time[0]->windDirection['code'], | |
'temperature'=>$weather->forecast->tabular->time[2]->temperature['value'], | |
'pressure'=>$weather->forecast->tabular->time[2]->pressure['value'] | |
), | |
Array( | |
'period'=>$period_text[intval($weather->forecast->tabular->time[3]['period'])], | |
'symbol'=>$weather->forecast->tabular->time[3]->symbol['number'], | |
'text'=>$sym_text["{$weather->forecast->tabular->time[3]->symbol['number']}"], | |
'precipitation'=>$weather->forecast->tabular->time[3]->precipitation['value'], | |
'wind'=>$weather->forecast->tabular->time[3]->windSpeed['mps'], | |
'wind_dir'=>$weather->forecast->tabular->time[3]->windDirection['code'], | |
'temperature'=>$weather->forecast->tabular->time[3]->temperature['value'], | |
'pressure'=>$weather->forecast->tabular->time[3]->pressure['value'] | |
), | |
Array( | |
'period'=>$period_text[intval($weather->forecast->tabular->time[4]['period'])], | |
'symbol'=>$weather->forecast->tabular->time[4]->symbol['number'], | |
'text'=>$sym_text["{$weather->forecast->tabular->time[4]->symbol['number']}"], | |
'precipitation'=>$weather->forecast->tabular->time[4]->precipitation['value'], | |
'wind'=>$weather->forecast->tabular->time[4]->windSpeed['mps'], | |
'wind_dir'=>$weather->forecast->tabular->time[4]->windDirection['code'], | |
'temperature'=>$weather->forecast->tabular->time[4]->temperature['value'], | |
'pressure'=>$weather->forecast->tabular->time[4]->pressure['value'] | |
) | |
) | |
) | |
); | |
$result = ob_get_contents(); | |
ob_end_clean(); | |
file_put_contents($cache_file,$result); | |
echo $result; | |
}else{ | |
echo file_get_contents($cache_file); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment