180) { $check = curl_data ($url_ct); $ct = json_decode ($rawData,true); # ------------------ these parts should be there if (!is_array ($ct) || !array_key_exists('observations',$ct) || !array_key_exists(0,$ct['observations']) ) { $arr = false;} // no correct data else { $mes = $ct['observations'][0]['metric']; unset ($ct['observations'][0]['metric']); $arr = array_merge ($ct['observations'][0],$mes); unset($ct , $mes);} } // eo load new data # if ($arr <> false) # --------------------- we have new current data { $epoch = $arr['epoch']; // station data time $md_arr = date('Ymd',$epoch); // for reset rain new day $ym_arr = date('Ym',$epoch); // for reset rain new month if (!array_key_exists('md',$data['hist']) ) { $data['hist']['md'] = $md_arr;} if (!array_key_exists('ym',$data['hist']) ) { $data['hist']['ym'] = $ym_arr;} $reset = false; // assume no reset if ($md_arr > $data['hist']['md']) { $reset = true; // new day -> add todays rain to month rain $rainMonth = $data['hist']['rainMnth'] + $data['hist']['rainToday'];} if ($ym_arr > $data['hist']['ym']) { $reset = true; // new month -> zero rain $rainMonth = 0;} if ($reset == true) # # ------------------- new day first current data # ------------------------------- into high-lows { $data['hist'] = array ( 'rainMnth' => $rainMonth, 'rainToday'=> $arr['precipTotal'], 'tempHigh' => $arr['temp'], 'TtempHigh' => $epoch, 'tempLow' => $arr['temp'], 'TtempLow' => $epoch, 'chilHigh' => $arr['windChill'], 'TchilHigh' => $epoch, 'chilLow' => $arr['windChill'], 'TchilLow' => $epoch, 'heatHigh' => $arr['heatIndex'], 'TheatHigh' => $epoch, 'heatLow' => $arr['heatIndex'], 'TheatLow' => $epoch, 'dewpHigh' => $arr['dewpt'], 'TdewpHigh' => $epoch, 'dewpLow' => $arr['dewpt'], 'TdewpLow' => $epoch, 'windHigh' => $arr['windSpeed'], 'TwindHigh' => $epoch, 'windLow' => $arr['windSpeed'], 'TwindLow' => $epoch, 'gustHigh' => $arr['windGust'], 'TgustHigh' => $epoch, 'gustLow' => $arr['windGust'], 'TgustLow' => $epoch, 'presHigh' => $arr['pressure'], 'TpresHigh' => $epoch, 'presLow' => $arr['pressure'], 'TpresLow' => $epoch, 'humiHigh' => $arr['humidity'], 'ThumiHigh' => $epoch, 'humiLow' => $arr['humidity'], 'ThumiLow' => $epoch, 'solrHigh' => $arr['solarRadiation'],'TsolrHigh' => $epoch, 'uvuvHigh' => $arr['uv'], 'TuvuvHigh' => $epoch ); $data['hist']['md'] = $md_arr; $data['hist']['ym'] = $ym_arr; } # # ---- current data is compared to cached values # ---------------to register new high-low values else { $data['hist']['rainToday'] = $arr['precipTotal']; $value = $arr['temp']; if ($value > $data['hist']['tempHigh']) { $data['hist']['tempHigh'] = $value; $data['hist']['TtempHigh'] = $epoch;} if ($value < $data['hist']['tempLow']) { $data['hist']['tempLow'] = $value; $data['hist']['TtempLow'] = $epoch;} $value = $arr['windChill']; if ($value > $data['hist']['chilHigh']) { $data['hist']['chilHigh'] = $value; $data['hist']['TchilHigh'] = $epoch;} if ($value < $data['hist']['chilLow']) { $data['hist']['chilLow'] = $value; $data['hist']['TchilLow'] = $epoch;} $value = $arr['heatIndex']; if ($value > $data['hist']['heatHigh']) { $data['hist']['heatHigh'] = $value; $data['hist']['TheatHigh'] = $epoch;} if ($value < $data['hist']['heatLow']) { $data['hist']['heatLow'] = $value; $data['hist']['TheatLow'] = $epoch;} $value = $arr['dewpt']; if ($value > $data['hist']['dewpHigh']) { $data['hist']['dewpHigh'] = $value; $data['hist']['TdewpHigh'] = $epoch;} if ($value < $data['hist']['dewpLow']) { $data['hist']['dewpLow'] = $value; $data['hist']['TdewpLow'] = $epoch;} $value = $arr['windSpeed']; if ($value > $data['hist']['windHigh']) { $data['hist']['windHigh'] = $value; $data['hist']['TwindHigh'] = $epoch;} if ($value < $data['hist']['windLow']) { $data['hist']['windLow'] = $value; $data['hist']['TwindLow'] = $epoch;} $value = $arr['windGust']; if ($value > $data['hist']['gustHigh']) { $data['hist']['gustHigh'] = $value; $data['hist']['TgustHigh'] = $epoch;} if ($value < $data['hist']['gustLow']) { $data['hist']['gustLow'] = $value; $data['hist']['TgustLow'] = $epoch;} $value = $arr['pressure']; if ($value > $data['hist']['presHigh']) { $data['hist']['presHigh'] = $value; $data['hist']['TpresHigh'] = $epoch;} if ($value < $data['hist']['presLow']) { $data['hist']['presLow'] = $value; $data['hist']['TpresLow'] = $epoch;} $value = $arr['humidity']; if ($value > $data['hist']['humiHigh']) { $data['hist']['humiHigh'] = $value; $data['hist']['ThumiHigh'] = $epoch;} if ($value < $data['hist']['humiLow']) { $data['hist']['humiLow'] = $value; $data['hist']['ThumiLow'] = $epoch;} $value = (float) $arr['solarRadiation']; if ($value > $data['hist']['solrHigh']) { $data['hist']['solrHigh'] = $value; $data['hist']['TsolrHigh'] = $epoch;} $value = $arr['uv']; if ($value > $data['hist']['uvuvHigh']) { $data['hist']['uvuvHigh'] = $value; $data['hist']['TuvuvHigh'] = $epoch;} } $trans = array(); $trans['stationDate'] = date('d-m-Y',$arr['epoch']) ; $trans['stationTime'] = date('H:i',$arr['epoch']) ; $trans['tempUnit'] = 'C'; $trans['humUnit'] = '%'; $trans['barUnit'] = 'hPa'; $trans['rainUnit'] = 'mm'; $trans['rateUnit'] = 'mm/h'; $trans['windUnit'] = 'km/h'; $sun_arr = date_sun_info(time(), $arr['lat'], $arr['lon']); $trans['sunriseTime'] = date('H:i',$sun_arr['sunrise']); $trans['sunsetTime'] = date('H:i',$sun_arr['sunset']);; $trans['outsideTemp'] = $arr['temp']; $trans['hiOutsideTemp'] = $data['hist']['tempHigh']; $trans['lowOutsideTemp'] = $data['hist']['tempLow']; $trans['hiOutsideTempTime'] = date('H:i',$data['hist']['TtempHigh']); $trans['lowOutsideTempTime'] = date('H:i',$data['hist']['TtempLow']); $trans['outsideHumidity'] = $arr['humidity']; $trans['hiHumidity'] = $data['hist']['humiHigh']; $trans['lowHumidity'] = $data['hist']['humiLow']; $trans['hiHumTime'] = date('H:i',$data['hist']['ThumiHigh']); $trans['lowHumTime'] = date('H:i',$data['hist']['ThumiLow']); $trans['outsideDewPt'] = $arr['dewpt']; $trans['windSpeed'] = $arr['windSpeed']; $trans['wind10Avg'] = $arr['windSpeed']; $trans['hiWindSpeed'] = $data['hist']['windHigh']; $trans['hiWindSpeedTime'] = date('H:i',$data['hist']['TwindHigh']); $trans['windDir'] = $arr['winddir']; $windlabel_shrt = array ('N','NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW','SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'); $degr = (int) $arr['winddir']; $key = (int) fmod((($degr + 11) / 22.5),16); $trans['windDirection'] = $windlabel_shrt[$key]; $trans['windChill'] = $arr['windChill']; $trans['outsideHeatIndex'] = $arr['heatIndex']; $trans['barometer'] = $arr['pressure']; $trans['hiBarometer'] = $data['hist']['presHigh']; $trans['lowBarometer'] = $data['hist']['presLow']; $trans['hiBarometerTime'] = date('H:i',$data['hist']['TpresHigh']); $trans['lowBarometerTime'] = date('H:i',$data['hist']['TpresLow']); $trans['dailyRain'] = $arr['precipTotal']; $trans['monthlyRain'] = $data['hist']['rainMnth'] + $arr['precipTotal']; $trans['rainRate'] = $arr['precipRate']; $trans['solarRad'] = $data['hist']['solrHigh']; $trans['hiSolarRadTime'] = date('H:i',$data['hist']['TsolrHigh']); $trans['uv'] = $arr['uv']; $trans['hiUV'] = $data['hist']['uvuvHigh']; $trans['hiUVTime'] = date('H:i',$data['hist']['TuvuvHigh']); $trans['version'] = 'WU-data '.date('YmdHis',$epoch); $data['last'] = $trans; $return = file_put_contents ($cached,serialize($data)); } else { $trans = $data['last'];} // no correct new data, use last one # #if (!array_key_exists('observations',$trans)) # { echo $rawData; # return;} # --------------------------- return latest data header("Content-type: text/plain"); echo 'BOF'.PHP_EOL; foreach ($trans as $key => $value) { echo '$'.$key.' = "'.$value.'";'.PHP_EOL;} echo 'EOF'.PHP_EOL; return; # ---------------------------------------------- # FUNCTIONS # ---------------------------------------------- # Load todays history from WU first time use # ---------------------------------------------- function load_data ($url_hl) { global $data, $rawData, $stck_lst; $rawData= ''; $check = curl_data ($url_hl); if ($check === false) { $data = false; return false;} $hilows = json_decode($rawData,true); if (!is_array($hilows) || !array_key_exists('observations',$hilows) ) { $data = false; return false;} # echo __LINE__.' '.$stck_lst.print_r($hilows,true); unset ($rawData); $epoch = time(); $hl = $hilows['observations'][0]['metric']; unset ($hilows['observations'][0]['metric']); $arr = array_merge ($hilows['observations'][0],$hl); #echo '
'.__LINE__.' '.print_r($arr,true);
        unset ($hilows, $hl);
        $data   = array ();
        $data['hist']   = array ( 
                'rainMnth' => 0,
                'rainToday'=> $arr['precipTotal'],
                'tempHigh' => $arr['tempHigh'],         'TtempHigh' => $epoch, 
                'tempLow'  => $arr['tempLow'],          'TtempLow'  => $epoch, 
                'chilHigh' => $arr['windchillHigh'],    'TchilHigh' => $epoch, 
                'chilLow'  => $arr['windchillLow'],     'TchilLow'  => $epoch, 
                'heatHigh' => $arr['heatindexHigh'],    'TheatHigh' => $epoch, 
                'heatLow'  => $arr['heatindexLow'],     'TheatLow'  => $epoch,
                'dewpHigh' => $arr['dewptHigh'],        'TdewpHigh' => $epoch, 
                'dewpLow'  => $arr['dewptLow'],         'TdewpLow'  => $epoch,

                'windHigh' => $arr['windspeedHigh'],    'TwindHigh' => $epoch, 
                'windLow'  => $arr['windspeedLow'],     'TwindLow'  => $epoch,
                'gustHigh' => $arr['windgustHigh'],     'TgustHigh' => $epoch, 
                'gustLow'  => $arr['windgustLow'],      'TgustLow'  => $epoch,
                'presHigh' => $arr['pressureMax'],      'TpresHigh' => $epoch, 
                'presLow'  => $arr['pressureMin'],      'TpresLow'  => $epoch, 
                'humiHigh' => $arr['humidityHigh'],     'ThumiHigh' => $epoch, 
                'humiLow'  => $arr['humidityLow'],      'ThumiLow'  => $epoch, 
                'solrHigh' => $arr['solarRadiationHigh'],'TsolrHigh'=> $epoch,
                'uvuvHigh' => $arr['uvHigh'],           'TuvuvHigh' => $epoch ) ;
        $data['hist']['md'] = date('Ymd', $epoch);
        $data['hist']['ym'] = date('Ym', $epoch); 
        $data['last']   = false;
        }
# ----------------------------------------------
#                        Download a file from WU
# ----------------------------------------------        
function curl_data ($url)
     {  global $stck_lst, $API, $rawData;
        $fake_url       = str_replace ($API, '_API_SETTING_',$url);     // hide API-key 
        $timeout        = 5;                                    // 5 seconds max wait for answer WU
        $start_time     =  microtime(true);
        $ch             = curl_init();                          // initialize a cURL session
        curl_setopt ($ch, CURLOPT_URL, $url);                   // connect to provided URL
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);           // verify peer off,removes a lot of errors with older hosts
        curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120424 Firefox/12.0 PaleMoon/12.0'); #### 2020-09-30
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);   // connection timeout
        curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);          // data timeout
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);        // return the data transfer
        curl_setopt ($ch, CURLOPT_NOBODY, false);               // do the download request without getting the body
        curl_setopt ($ch, CURLOPT_HEADER, false);               // include header information
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);         // follow Location: redirect allowed
        curl_setopt($ch, CURLOPT_MAXREDIRS, 1);                 //   but only one time
        $rawData= curl_exec ($ch);
        $info	= curl_getinfo($ch);
        $error  = curl_error($ch); # echo print_r($rawData,true).print_r($error,true).print_r($info,true); exit;
        curl_close ($ch);
        $now    = microtime(true);
        $passed = $now - $start_time;
        if ($passed < 0.0001) {$string1 = '< 0.0001';} else {$string1 = round($passed,4);}
        $CHECK_HTTP_CODES = array ('204','404', '429','502', '500');
        if (in_array ($info['http_code'],$CHECK_HTTP_CODES) ) 
             {  $stck_lst .= basename(__FILE__).' ('.__LINE__.') time spent: '.$string1.' - PROBLEM => http_code: '.$info['http_code'].', no valid data for '.$fake_url.PHP_EOL;
                return false;}
        if ($error <> '')
             {  $stck_lst .= basename(__FILE__).' ('.__LINE__.') time spent: '.$string1.' -  invalid CURL '.$error.' '.$fake_url.PHP_EOL; 
                return false;}        
        else {  $stck_lst .= basename(__FILE__).' ('.__LINE__.') time spent: '.$string1.' -  CURL OK for '.$fake_url.PHP_EOL; }  #echo __LINE__.' '.$stck_lst.print_r($info,true).print_r($error,true); die;
        return true;
        }