/**
  * @param Location $location
  * @return Weather
  * @throws WeatherProviderException
  */
 public function fetch(Location $location) : Weather
 {
     $client = new Client(['base_uri' => 'http://api.openweathermap.org/data/2.5/', 'timeout' => 2.0]);
     $response = $client->request('GET', 'weather?q=' . $location->getCity() . '&appid=' . $this->getAPI() . '&units=metric');
     $data = $response->getBody();
     if ($data == NULL) {
         throw new WeatherProviderException('No data received from OpenWeatherMap Provider');
     }
     return $this->parser->parse($data);
 }
Example #2
0
 /**
  * @param Location $location
  * @return Weather
  * @throws WeatherProviderException
  */
 public function fetch(Location $location) : Weather
 {
     $BASE_URL = "http://query.yahooapis.com/v1/public/yql";
     $yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1)
       where text = "' . $location->getCity() . '") and u="c"';
     $yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
     $session = curl_init($yql_query_url);
     curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
     $data = curl_exec($session);
     if ($data == NULL) {
         throw new WeatherProviderException('No data received from Yahoo Provider');
     }
     return $this->parser->parse($data);
 }
Example #3
0
 /**
  * @param Location $location
  * @return Weather
  */
 public function fetch(Location $location) : Weather
 {
     $fileDirectory = 'cached/';
     if (!is_dir($fileDirectory)) {
         mkdir($fileDirectory, 0775, true);
     }
     $filename = $fileDirectory . $location->getCity() . '.txt';
     if (file_exists($filename)) {
         $file = file($filename);
         $dateNow = new \DateTime('now');
         $dateNow = $dateNow->getTimestamp();
         $dateThen = $file[0];
         if (intval($dateNow) - intval($dateThen) >= $this->ttl) {
             //printf("New values were taken:");
             return $this->writeToFile($this->ProviderFetchWeather($location), $filename);
         } else {
             //printf("Information from cached file: sena: %d  dabar %d laiko skirtumas: %d", $dateThen, $dateNow, $this->ttl);
             //var_dump($filename);
             return new Weather(trim($file[1]), trim($file[2]), $file[3]);
         }
     }
     return $this->writeToFile($this->ProviderFetchWeather($location), $filename);
 }