You are here

function _weather_retrieve_data in Weather 6.5

Same name and namespace in other branches
  1. 5.6 weather.module \_weather_retrieve_data()
  2. 5 weather.module \_weather_retrieve_data()

Retrieve data from http://www.aviationweather.gov/

1 call to _weather_retrieve_data()
weather_get_metar in ./weather.module
Fetches the latest METAR data from the database or internet

File

./weather.module, line 2123
Display <acronym title="METeorological Aerodrome Report">METAR</acronym> weather data from anywhere in the world

Code

function _weather_retrieve_data($icao) {
  $metar_raw = FALSE;

  // Specify timeout in seconds
  $timeout = 10;
  $url = 'http://www.aviationweather.gov/adds/metars/?chk_metars=on&station_ids=' . $icao;
  $response = drupal_http_request($url, array(
    'timeout' => $timeout,
  ));

  // Extract the valid METAR data from the received webpage.
  if (preg_match("/({$icao} [0-9]{6}Z [^<]+)/m", $response->data, $matches)) {
    $metar_raw = str_replace("\n", "", $matches[1]);
  }

  // Check for errors.
  if ($metar_raw === FALSE) {

    // Make an entry about this error into the watchdog table.
    watchdog('weather', 'Download location for METAR data is not accessible.', array(), WATCHDOG_ERROR);

    // Show a message to users with administration priviledges
    if (user_access('administer custom weather block') or user_access('administer site configuration')) {
      drupal_set_message(t('Download location for METAR data is not accessible.'), 'error');
    }
  }
  return $metar_raw;
}