You are here

function weather_retrieve_data in Weather 7

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

Parameters

string $icao: ICAO code

Return value

string Raw METAR string or FALSE

1 call to weather_retrieve_data()
weather_refresh_data in ./weather_parser.inc
Parses raw METAR data string and stores results in database.

File

./weather_parser.inc, line 119
Retrieves and parses raw METAR data and stores result in database.

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 (!empty($response->data) && 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;
}