You are here

public function LiveWeather::locationCheck in Live Weather 8.2

Same name and namespace in other branches
  1. 8 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') {
  $settings = \Drupal::config('live_weather.settings')
    ->get('settings');
  $data = '';
  $url = 'https://weather-ydn-yql.media.yahoo.com/forecastrss';
  $app_id = isset($settings['app_id']) ? $settings['app_id'] : '';
  $consumer_key = isset($settings['consumer_key']) ? $settings['consumer_key'] : '';
  $consumer_secret = isset($settings['consumer_secret']) ? $settings['consumer_secret'] : '';
  if (!empty($app_id) && !empty($consumer_key) && !empty($consumer_secret)) {
    $query = array(
      'woeid' => $woeid,
      'format' => 'json',
    );
    $oauth = array(
      'oauth_consumer_key' => $consumer_key,
      'oauth_nonce' => uniqid(mt_rand(1, 1000)),
      'oauth_signature_method' => 'HMAC-SHA1',
      'oauth_timestamp' => time(),
      'oauth_version' => '1.0',
    );
    $base_info = $this
      ->buildBaseString($url, 'GET', array_merge($query, $oauth));
    $composite_key = rawurlencode($consumer_secret) . '&';
    $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
    $oauth['oauth_signature'] = $oauth_signature;
    $header = array(
      $this
        ->buildAuthorizationHeader($oauth),
      'Yahoo-App-Id: ' . $app_id,
    );
    $options = array(
      CURLOPT_HTTPHEADER => $header,
      CURLOPT_HEADER => false,
      CURLOPT_URL => $url . '?' . http_build_query($query),
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_SSL_VERIFYPEER => false,
    );
    $ch = curl_init();
    curl_setopt_array($ch, $options);
    $response = curl_exec($ch);
    curl_close($ch);
    if (!empty($response)) {
      $data = Json::decode($response);
      if (empty($data) || !is_array($data)) {
        $this->logger
          ->warning('Failed to get data. Please check your API details.');
      }
    }
  }
  return $data;
}