You are here

public function LiveWeather::locationCheck in Live Weather 8

Same name and namespace in other branches
  1. 8.2 src/LiveWeather.php \Drupal\live_weather\LiveWeather::locationCheck()

Get location data.

Overrides LiveWeatherInterface::locationCheck

File

src/LiveWeather.php, line 60
Contains \Drupal\live_weather\LiveWeather.

Class

LiveWeather
Live weather.

Namespace

Drupal\live_weather

Code

public function locationCheck($woeid = NULL, $filter = '', $unit = 'f') {
  $data = '';
  $url = 'https://query.yahooapis.com/v1/public/yql?q=select ' . $filter . ' from weather.forecast where woeid IN (' . $woeid . ') and u= "' . $unit . '"&format=json';
  try {
    $response = $this->httpClient
      ->get($url);
    $data = (string) $response
      ->getBody();
  } catch (RequestException $e) {
    $this->logger
      ->warning('Failed to get data "%error".', array(
      '%error' => $e
        ->getMessage(),
    ));
    return;
  } catch (BadResponseException $e) {
    $this->logger
      ->warning('Failed to get data "%error".', array(
      '%error' => $e
        ->getMessage(),
    ));
    return;
  } catch (\Exception $e) {
    $this->logger
      ->warning('Failed to get data "%error".', array(
      '%error' => $e
        ->getMessage(),
    ));
    return;
  }
  if (!empty($data)) {
    $data = Json::decode($data);
  }
  return $data;
}