yr-no.php 'Szlcsend', 'Light air' => 'Leheletszer', 'Light breeze' => 'Gyenge szl', 'Gentle breeze' => 'Szl', 'Moderate breeze' => 'Eros szl', 'Fresh breeze' => 'Viharos szl', 'Strong breeze' => 'Strong breeze', 'Near Gale' => 'Near Gale', 'Fresh Gale' => 'Viharos szl', 'Strong Gale' => 'Szvihar', 'Storm' => 'Orkn', '$raw' => '$raw'); return $txtspeeds[$raw]; } function winddirs($raw) { $txtdirs = array( 'south' => 'Dlkeleti', 'south-southwest' => 'Dl-dlnyugati', 'southwest' => 'Dlnyugati', 'west-southwest' => 'Nyugat-dlnyugati', 'west' => 'Nyugati', 'west-northwest' => 'Nyugat-szaknyugati', 'northwest' => 'Nyugat-szaknyugati', 'north-northwest' => 'szak-szaknyugat', 'north' => 'szaki', 'north-northeast' => 'szak-szakkeleti', 'northeast' => 'szaki', 'east-northeast' => 'Kelet-szakkeleti', 'east' => 'Keleti', 'east-southeast' => 'Kelet-dlkeletre', 'southeast' => 'Dlkeleti', 'south-southeast' => 'Dl-dlkeleti', '$raw' => '$raw'); return $txtdirs[$raw]; } //***************** END SETTINGS ***************************** $qa_URL = $URL . 'varsel.xml'; // refresh cached copy of page if needed // fetch/cache code by Tom at carterlake.org if (file_exists($qacacheName) and filemtime($qacacheName) + $qarefetchSeconds > time()) { $WhereLoaded = "from cache $qacacheName"; $html = implode('', file($qacacheName)); } else { $WhereLoaded = "from URL $qa_URL"; $html = fetchqaUrlWithoutHanging($qa_URL); $fp = fopen($qacacheName, "w"); if($fp) { list($headers,$content) = explode("\r\n\r\n",$html); // split headers from html $i = strpos($html,"\r\n\r\n"); $headers = substr($html,0,$i-1); $content = substr($html,$i+2); //$content = parseToXML($content); $content = trim($content); $write = fputs($fp, $content); fclose($fp); } else { print "\n"; } } print "\n"; if(strlen($html) < 50 ) { print "\n"; return; } $xml=xml2ary(file_get_contents('salo.xml')); $xml = $xml['weatherdata'][_c]; //print_r($xml); $location = $xml[location]; $city = $location[_c][name][_v]; $country = $location[_c][country][_v]; $alt = $location[_c][location][_a][altitude]; $lat = $location[_c][location][_a][latitude]; $lon = $location[_c][location][_a][longitude]; $off = $location[_c][timezone][_a][utcoffsetMinutes]; //echo "$lat $lon $off
"; $offset = ($off / 60); $zenith=90+33/60; $sunri = date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $lon, $zenith, $offset); $sunse = date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $lon, $zenith, $offset); $sunr = strtotime($sunri); $suns = strtotime($sunse); if($sunri == '') { $sunri = "--"; } if($sunse == '') { $sunse = "--"; } //echo "$offset $sunr $suns
"; $meta = $xml[meta]; $lupd = $meta[_c][lastupdate][_v]; $lupd = date('d.m.Y @ H.i', parse_time($lupd)); $nupd = $meta[_c][nextupdate][_v]; $nupd = date('d.m.Y @ H.i', parse_time($nupd)); $forecasts = $xml[forecast][_c][tabular][_c][time]; $howmany = count($forecasts); echo "\n"; //echo "$city $country $alt $lupd $nupd

"; $tablehead = ' '; $table = '
' . DAY . '     ' . TEMPE . ' ' . PRECIP . ' ' . BAROI . ' ' . WINDI . '  
' . FORECAST_FOR . ' ' . $city . ', ' . $country . '
' . LASTUPD . ': ' . $lupd . '
' . NEXTUPD . ': ' . $nupd . '
' . PROVIDED_BY . ': yr.no' . $extlink . '
' . MADE_BY . ': ' . NORMETEO . '' . $extlink . ' & NRK' . $extlink . '
'; $table .= ''; for ($i = 0; $i < $howmany; $i++) { $validfr = $forecasts[$i][_a][from]; $validfrd = date('d.m.Y', parse_time($validfr)); $validfrt = date('H.i', parse_time($validfr)); $validto = $forecasts[$i][_a][to]; $validtot = date('H.i', parse_time($validto)); $period = $forecasts[$i][_a][period]; $period = $period + 1; $icon = $forecasts[$i][_c][symbol][_a][number]; $micons = array('1', '2', '3', '5', '6', '7', '8'); $rh = date('H', parse_time($validto)); $sh = date('H', parse_time($validfr)); if ((date('H', $sunr)) > ($rh - 3)) { $usunrise = 1; } else if (((date('H', $suns)) < ($sh + 3)) && ($sh > 18)) { $usunrise = 1; } else { $usunrise = 0; } if (($usunrise == 1) && (in_array($icon, $micons))) { $nicon = $icon . 'n.png'; } else { $nicon = $icon . '.png'; } $precip = $forecasts[$i][_c][precipitation][_a][value]; $dir = $forecasts[$i][_c][windDirection][_a][code]; $dirname = $forecasts[$i][_c][windDirection][_a][name]; $spd = $forecasts[$i][_c][windSpeed][_a][mps]; $spdname = $forecasts[$i][_c][windSpeed][_a][name]; $temp = $forecasts[$i][_c][temperature][_a][value]; $baro = $forecasts[$i][_c][pressure][_a][value]; if($temp <= 0) { $tcolor = "below"; } else { $tcolor = "over"; } if(($precip > 0) && ($precip <= 1)) { $prc = "< 1 mm"; } else if($precip > 1) { $prc = round($precip) . " mm"; } else { $prc = " "; } $wind = windspeed($spdname) . ' ' . FROM . ' ' . winddirs(strtolower($dirname)) . '' . WEXT; $curr = date('d.m.Y', time()); if($curr == $validfrd) { $day = TODAY; $ik = 0; } else if(($i < 1) && ($validfrd <> $curr)) { $day = TOMORROW; $ik = 0; } else { $day = $validfrd; $ik = 1; } $pr = parse_daytime($period); if ($validfrd <> $vlc) { $table .= ''; } if (($validfrd <> $vlc) && ($i > 0) && ($i < 5)) { $table .= '

meteogram

' . NEXTFOUR . '

' .$tablehead . ''; } if (($validfrd <> $vlc) && ($plc == 4) && ($period ==3)) { $table .= '

' . LASTDAYS . '


' .$tablehead . ''; } if (($validfrd <> $vlc) && ($period == 3)) { $table .= ' ' . $day . '   ' . $temp . '°C ' . parseTohtml($prc) . ' ' . round($baro) . ' hPa ' . $wind . ' ' . round($spd) . ' m/s '; } else if (($validfrd <> $vlc) && ($i == 0)) { $table .= ' ' . $day . ' ' . $validfrt . ' - ' . $validtot . ' ' . $temp . '°C ' . parseTohtml($prc) . ' ' . round($baro) . ' hPa ' . $wind . ' ' . round($spd) . ' m/s '; } else if (($validfrd <> $vlc)) { $table .= ' ' . $day . ' ' . $validfrt . ' - ' . $validtot . ' ' . $temp . '°C ' . parseTohtml($prc) . ' ' . round($baro) . ' hPa ' . $wind . ' ' . round($spd) . ' m/s '; } else { $table .= '   ' . $validfrt . ' - ' . $validtot . ' ' . $temp . '°C ' . parseTohtml($prc) . ' ' . round($baro) . ' hPa ' . $wind . ' ' . round($spd) . ' m/s '; } $plc = $period; $vlc = $validfrd; } $table .= ''; echo $table; // ----------------------------functions ----------------------------------- function parse_daytime($raw) { if($raw == 1) { $dttxt = NIGHT; } if($raw == 2) { $dttxt = MORNING; } if($raw == 3) { $dttxt = DAY; } if($raw == 4) { $dttxt = EVENING; } return $dttxt; } function parseTohtml($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); return $xmlStr; } function wspeed($spd) { $spd = round($spd); if($spd == 0) { $spdtxt = CALM; } if(($spd > 0) && ($spd < 1.4)) { $spdtxt = WWLIGHT; } if(($spd > 1.4) && ($spd < 3.5)) { $spdtxt = WLIGHT; } if(($spd >= 3.5) && ($spd < 5.5)) { $spdtxt = WBLIGHT; } if(($spd >= 5.5) && ($spd < 8)) { $spdtxt = WMLIGHT; } if(($spd >= 8) && ($spd < 13.9)) { $spdtxt = WMODERATE; } if(($spd >= 13.9) && ($spd < 20.8)) { $spdtxt = WHARD; } if($spd >= 20.8) { $spdtxt = WSTORM; } return $spdtxt; } function parse_time($raw) { $yr = substr($raw,0,4); $mt = substr($raw,5,2); $da = substr($raw,8,2); $hr = substr($raw,11,2); $mi = substr($raw,14,2); $tm = mktime($hr,$mi,00,$mt,$da,$yr); return $tm; } function parseToXML($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr); $xmlStr=str_replace('"','"',$xmlStr); $xmlStr=str_replace("'",''',$xmlStr); $xmlStr=str_replace("&",'&',$xmlStr); return $xmlStr; } function fetchqaUrlWithoutHanging($url) // thanks to Tom at Carterlake.org for this script fragment { // Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed $numberOfSeconds=4; // Suppress error reporting so Web site visitors are unaware if the feed fails error_reporting(0); // Extract resource path and domain from URL ready for fsockopen $url = str_replace("http://","",$url); $urlComponents = explode("/",$url); $domain = $urlComponents[0]; $resourcePath = str_replace($domain,"",$url); // Establish a connection $socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds); if (!$socketConnection) { // You may wish to remove the following debugging line on a live Web site print("\n"); } // end if else { $xml = ''; fputs($socketConnection, "GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n"); // Loop until end of file while (!feof($socketConnection)) { $xml .= fgets($socketConnection, 4096); } // end while fclose ($socketConnection); } // end else return($xml); } // end function // XML to Array function xml2ary(&$string) { $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parse_into_struct($parser, $string, $vals, $index); xml_parser_free($parser); $mnary=array(); $ary=&$mnary; foreach ($vals as $r) { $t=$r['tag']; if ($r['type']=='open') { if (isset($ary[$t])) { if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array()); $cv=&$ary[$t][count($ary[$t])-1]; } else $cv=&$ary[$t]; if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;} $cv['_c']=array(); $cv['_c']['_p']=&$ary; $ary=&$cv['_c']; } elseif ($r['type']=='complete') { if (isset($ary[$t])) { // same as open if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array()); $cv=&$ary[$t][count($ary[$t])-1]; } else $cv=&$ary[$t]; if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;} $cv['_v']=(isset($r['value']) ? $r['value'] : ''); } elseif ($r['type']=='close') { $ary=&$ary['_p']; } } _del_p($mnary); return $mnary; } // _Internal: Remove recursion in result array function _del_p(&$ary) { foreach ($ary as $k=>$v) { if ($k==='_p') unset($ary[$k]); elseif (is_array($ary[$k])) _del_p($ary[$k]); } } // Array to XML function ary2xml($cary, $d=0, $forcetag='') { $res=array(); foreach ($cary as $tag=>$r) { if (isset($r[0])) { $res[]=ary2xml($r, $d, $tag); } else { if ($forcetag) $tag=$forcetag; $sp=str_repeat("\t", $d); $res[]="$sp<$tag"; if (isset($r['_a'])) {foreach ($r['_a'] as $at=>$av) $res[]=" $at=\"$av\"";} $res[]=">".((isset($r['_c'])) ? "\n" : ''); if (isset($r['_c'])) $res[]=ary2xml($r['_c'], $d+1); elseif (isset($r['_v'])) $res[]=$r['_v']; $res[]=(isset($r['_c']) ? $sp : '')."\n"; } } return implode('', $res); } // Insert element into array function ins2ary(&$ary, $element, $pos) { $ar1=array_slice($ary, 0, $pos); $ar1[]=$element; $ary=array_merge($ar1, array_slice($ary, $pos)); } // ---------------------------------------------------------- ?>