Пример #1
0
 public function getWeatherDetailData($userLocation = '')
 {
     global $toTemp, $toWind, $toRain, $toBaro, $pageName, $cacheDir, $uomTemp, $uomWind, $uomRain, $uomBaro;
     #----------------------------------------------------------------------------------------------
     # clean user input
     #----------------------------------------------------------------------------------------------
     $userLocation = trim($userLocation);
     $this->location = $userLocation;
     $filename = str_replace('/', '', $userLocation);
     $this->cachePath = $cacheDir;
     $uoms = $toTemp . $toWind . $toRain . $toBaro;
     $this->cacheFile = $this->cachePath . $pageName . '-' . $this->lang . '-' . $filename . '-' . $uoms;
     #----------------------------------------------------------------------------------------------
     # try loading data from cache
     #----------------------------------------------------------------------------------------------
     $returnArray = $this->loadFromCache();
     // load from cache returns data only when its data is valid
     if (!empty($returnArray)) {
         return $returnArray;
         // if data is in cache and valid return data to calling program
     }
     // eo valid data, return to calling program
     #----------------------------------------------------------------------------------------------
     # combine everything into required url
     #----------------------------------------------------------------------------------------------
     #  http://www.yr.no/place/Canada/Other/Toronto/forecast_hour_by_hour.xml
     $this->apiUrlpart[1] = $this->location;
     $this->weatherApiUrl = '';
     for ($i = 0; $i < count($this->apiUrlpart); $i++) {
         $this->weatherApiUrl .= $this->apiUrlpart[$i];
     }
     if (!$this->makeRequest()) {
         // load xml from url and process
         ws_message('<h3>ERROR yrnoCreateDetailArr.php (' . __LINE__ . '): Unable to retrieve xml for ' . $this->weatherApiUrl . '</h3>', true);
         return false;
     }
     $xml = new SimpleXMLElement($this->rawData);
     //			print_r ($xml);
     $returnDetails = array();
     #--------------------------------------------------------------------------------------------------
     # first, get and save request info / units etc
     #--------------------------------------------------------------------------------------------------
     $returnDetails['request_info']['type'] = 'xml';
     $returnDetails['request_info']['city'] = (string) $xml->location->name . '-' . $xml->location->country;
     $returnDetails['request_info']['logo'] = (string) $xml->credit->link['text'];
     $returnDetails['request_info']['link'] = (string) $xml->credit->link['url'];
     $returnDetails['request_info']['uomTemp'] = $this->uomTemp;
     $returnDetails['request_info']['uomWind'] = $this->uomWind;
     $returnDetails['request_info']['uomRain'] = $this->uomRain;
     $returnDetails['request_info']['uomBaro'] = $this->uomBaro;
     $returnDetails['request_info']['lastupdate'] = (string) $xml->meta->lastupdate;
     $string = (string) $xml->meta->nextupdate;
     $returnDetails['request_info']['nextupdate'] = $string;
     $returnDetails['request_info']['nextupdateunix'] = strtotime($string);
     #--------------------------------------------------------------------------------------------------
     #  get forecast info
     #--------------------------------------------------------------------------------------------------
     $end = count($xml->forecast->tabular->time);
     // one forecast is one occurence of <time>...</time>
     for ($i = 0; $i < $end; $i++) {
         # <time from="2013-05-08T11:00:00" to="2013-05-08T14:00:00">
         $returnDetails['forecast'][$i]['date'] = substr((string) $xml->forecast->tabular->time[$i - 1]['to'], 0, 10);
         $returnDetails['forecast'][$i]['timeFrom'] = strtotime((string) $xml->forecast->tabular->time[$i - 1]['from']);
         $returnDetails['forecast'][$i]['timeTo'] = $returnDetails['forecast'][$i]['timestamp'] = strtotime((string) $xml->forecast->tabular->time[$i - 1]['to']);
         $data = $xml->forecast->tabular->time[$i - 1];
         # <symbol number="3" name="Partly cloudy" var="03d"/>
         $returnDetails['forecast'][$i]['icon'] = (string) $data->symbol['number'];
         $returnDetails['forecast'][$i]['weatherDesc'] = (string) $data->symbol['name'];
         # <temperature unit="celsius" value="18"/>
         $string = (string) $data->temperature['value'];
         $amount = yrnoConvertTemp($string, $this->uomTemp);
         $returnDetails['forecast'][$i]['tempNU'] = (string) $amount;
         $returnDetails['forecast'][$i]['temp'] = (string) $amount . $uomTemp;
         # <windSpeed mps="4.7" name="Gentle breeze"/>
         $string = (string) $data->windSpeed['mps'];
         $amount = yrnoConvertWind($string, $this->uomWind);
         $returnDetails['forecast'][$i]['windSpeedNU'] = (string) $amount;
         $returnDetails['forecast'][$i]['beaufort'] = yrnobeaufort($string, $this->uomWind);
         $returnDetails['forecast'][$i]['windSpeed'] = (string) $amount . $uomWind;
         $returnDetails['forecast'][$i]['windTxt'] = (string) $data->windSpeed['name'];
         # <windDirection deg="103.6" code="ESE" name="East-southeast"/>
         $returnDetails['forecast'][$i]['windDir'] = (string) $data->windDirection['code'];
         $returnDetails['forecast'][$i]['windDeg'] = (string) $data->windDirection['deg'];
         # <precipitation value="0"/>
         if (isset($data->precipitation['maxvalue'])) {
             $amount = $data->precipitation['minvalue'];
             $string = (string) yrnoConvertRain($amount, $this->uomRain);
             $amount = $data->precipitation['maxvalue'];
             $string .= '-' . (string) yrnoConvertRain($amount, $this->uomRain);
         } else {
             $amount = $data->precipitation['value'];
             $string = (string) yrnoConvertRain($amount, $this->uomRain);
         }
         $returnDetails['forecast'][$i]['rainNU'] = $string;
         $returnDetails['forecast'][$i]['rain'] = $string . $uomRain;
         # <pressure unit="hPa" value="1014.8"/>
         $string = (string) $data->pressure['value'];
         $amount = round(yrnoConvertBaro($string, $this->uomBaro));
         $returnDetails['forecast'][$i]['baroNU'] = (string) $amount;
         $returnDetails['forecast'][$i]['baro'] = (string) $amount . $uomBaro;
     }
     // eo for loop forecasts
     if (!isset($returnDetails['forecast']) || count($returnDetails['forecast']) < 3) {
         return $returnDetails;
     }
     $this->writeToCache($returnDetails);
     return $returnDetails;
 }
Пример #2
0
 public function getWeatherData($userLocation = '')
 {
     global $toTemp, $toWind, $toRain, $toBaro, $pageName, $cacheDir, $uomTemp, $uomWind, $uomRain, $uomBaro;
     #----------------------------------------------------------------------------------------------
     # clean user input
     #----------------------------------------------------------------------------------------------
     $userLocation = trim($userLocation);
     $this->location = $userLocation;
     $filename = str_replace('/', '', $userLocation);
     $this->cachePath = $cacheDir;
     $uoms = $toTemp . $toWind . $toRain . $toBaro;
     $this->cacheFile = $this->cachePath . $pageName . '-' . $this->lang . '-' . $filename . '-' . $uoms;
     #----------------------------------------------------------------------------------------------
     # try loading data from cache
     #----------------------------------------------------------------------------------------------
     if ($this->enableCache) {
         $returnArray = $this->loadFromCache();
         // load from cache returns data only when its data is valid
         if (!empty($returnArray)) {
             return $returnArray;
             // if data is in cache and valid return data to calling program
         }
         // eo valid data, return to calling program
     }
     // eo check cache
     #----------------------------------------------------------------------------------------------
     # combine everything into required url
     #----------------------------------------------------------------------------------------------
     #  http://www.yr.no/place/Belgium/Flanders/Wilsele/varsel.xml
     $this->apiUrlpart[1] = $this->location;
     $this->weatherApiUrl = '';
     for ($i = 0; $i < count($this->apiUrlpart); $i++) {
         $this->weatherApiUrl .= $this->apiUrlpart[$i];
     }
     if (!$this->makeRequest()) {
         // load xml from url, if fails stop
         ws_message('<h3>ERROR module yrnoCreateArr.php (' . __LINE__ . '): Unable to retrieve xml for ' . $this->weatherApiUrl . '</h3>', true);
         return false;
     }
     $xml = new SimpleXMLElement($this->rawData);
     $returnArray = array();
     #--------------------------------------------------------------------------------------------------
     # first, get and save request info / units etc
     #--------------------------------------------------------------------------------------------------
     $returnArray['request_info']['type'] = 'xml';
     $returnArray['request_info']['city'] = (string) $xml->location->name . '-' . $xml->location->country;
     $returnArray['request_info']['logo'] = (string) $xml->credit->link['text'];
     $returnArray['request_info']['link'] = (string) $xml->credit->link['url'];
     $returnArray['request_info']['uomTemp'] = $this->uomTemp;
     $returnArray['request_info']['uomWind'] = $this->uomWind;
     $returnArray['request_info']['uomRain'] = $this->uomRain;
     $returnArray['request_info']['uomBaro'] = $this->uomBaro;
     $returnArray['request_info']['uomDistance'] = 'n/a';
     $returnArray['request_info']['lastupdate'] = (string) $xml->meta->lastupdate;
     $returnArray['request_info']['nextupdate'] = (string) $xml->meta->nextupdate;
     $returnArray['request_info']['nextupdate'] = (string) $xml->meta->nextupdate;
     $returnArray['request_info']['nextupdateunix'] = strtotime($returnArray['request_info']['nextupdate']);
     #--------------------------------------------------------------------------------------------------
     # YR.NO only supplies forecast, no current condition descriptions
     #--------------------------------------------------------------------------------------------------
     #--------------------------------------------------------------------------------------------------
     #  get forecast info
     #--------------------------------------------------------------------------------------------------
     $end = count($xml->forecast->tabular->time);
     // one forecast is one occurence of <time>...</time>
     for ($i = 0; $i < $end; $i++) {
         # <time from="2013-05-08T05:00:00" to="2013-05-08T11:00:00" period="1">
         $returnArray['forecast'][$i]['date'] = substr((string) $xml->forecast->tabular->time[$i]['from'], 0, 10);
         $returnArray['forecast'][$i]['timeFrom'] = strtotime((string) $xml->forecast->tabular->time[$i]['from']);
         $returnArray['forecast'][$i]['timeTo'] = $returnArray['forecast'][$i]['timestamp'] = strtotime((string) $xml->forecast->tabular->time[$i]['to']);
         $returnArray['forecast'][$i]['hour'] = (string) $xml->forecast->tabular->time[$i]['period'];
         if (!isset($offset_0)) {
             $period = $returnArray['forecast'][$i]['hour'];
             $string = (string) $xml->forecast->tabular->time[$i]['to'];
             list($none, $time) = explode('T', $string);
             $hour = explode(':', $time);
             if ($period == '0') {
                 $offset_0 = 6 - $hour[0];
                 $returnArray['request_info']['offset'] = $offset_0;
                 echo '<!-- offset night set to ' . $offset_0 . ' hours -->' . PHP_EOL;
             }
         }
         $data = $xml->forecast->tabular->time[$i];
         # <symbol number="2" name="Fair" var="02d"/>
         $returnArray['forecast'][$i]['icon'] = (string) $data->symbol['number'];
         // = icon number
         $returnArray['forecast'][$i]['weatherDesc'] = (string) $data->symbol['name'];
         # <temperature unit="celsius" value="11"/>
         $string = (string) $data->temperature['value'];
         $amount = yrnoConvertTemp($string, $this->uomTemp);
         $returnArray['forecast'][$i]['tempNU'] = (string) $amount;
         $returnArray['forecast'][$i]['temp'] = (string) $amount . $uomTemp;
         # <windSpeed mps="3.4" name="Gentle breeze"/>
         $string = (string) $data->windSpeed['mps'];
         $amount = yrnoConvertWind($string, $this->uomWind);
         $returnArray['forecast'][$i]['windSpeedNU'] = (string) $amount;
         $returnArray['forecast'][$i]['beaufort'] = yrnobeaufort($string, $this->uomWind);
         $returnArray['forecast'][$i]['windSpeed'] = (string) $amount . $uomWind;
         $returnArray['forecast'][$i]['windTxt'] = (string) $data->windSpeed['name'];
         # <windDirection deg="105.3" code="ESE" name="East-southeast"/>
         $returnArray['forecast'][$i]['windDir'] = (string) $data->windDirection['code'];
         $returnArray['forecast'][$i]['windDeg'] = (string) $data->windDirection['deg'];
         # <precipitation value="0.9"/>
         if (isset($data->precipitation['maxvalue'])) {
             $amount = $data->precipitation['minvalue'];
             $string = (string) yrnoConvertRain($amount, $this->uomRain);
             $amount = $data->precipitation['maxvalue'];
             $string .= '-' . (string) yrnoConvertRain($amount, $this->uomRain);
         } else {
             $amount = $data->precipitation['value'];
             $string = (string) yrnoConvertRain($amount, $this->uomRain);
         }
         $returnArray['forecast'][$i]['rainNU'] = $string;
         $returnArray['forecast'][$i]['rain'] = $string . $uomRain;
         # <pressure unit="hPa" value="1014.8"/>
         $string = (string) $data->pressure['value'];
         $amount = yrnoConvertBaro($string, $this->uomBaro);
         $returnArray['forecast'][$i]['baroNU'] = (string) $amount;
         $returnArray['forecast'][$i]['baro'] = (string) $amount . $uomBaro;
     }
     // eo for loop forecasts
     if (!isset($returnArray['forecast']) || count($returnArray['forecast']) < 3) {
         return $returnArray;
     }
     if ($this->enableCache) {
         $this->writeToCache($returnArray);
     }
     return $returnArray;
 }