You are here

function _yr_verdata_fetch_xml in Yr Weatherdata 6.2

Same name and namespace in other branches
  1. 7 yr_verdata.module \_yr_verdata_fetch_xml()

Function for getting an xml file from yr.no.

Parameters

$location: A location object from the {yr_verdata} table.

1 call to _yr_verdata_fetch_xml()
_yr_verdata_refresh_xml in ./yr_verdata.module
Function for updating an xml file from yr.no on cron run.

File

./yr_verdata.module, line 1114
yr_verdata.module This file contains the code for getting the forecast from yr.no and displaying it on a Drupal site.

Code

function _yr_verdata_fetch_xml($location) {
  $feed = check_url('http://www.yr.no/' . drupal_urlencode(drupal_substr(rawurldecode($location->url), 17)) . 'varsel.xml');

  // Initialize new cURL session.
  $ch = curl_init();

  // Test the connection and see if the returned data is actually a valid forecast.
  curl_setopt($ch, CURLOPT_URL, $feed);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  $data = curl_exec($ch);
  $info = curl_getinfo($ch);
  $valid_xml = simplexml_load_string($data);
  if ($info['http_code'] == '200' && isset($valid_xml->location->name)) {

    // The forecast seems ok, we can return it.
    if (variable_get('yr_verdata_debug', 0) == 1) {
      watchdog('yr_verdata', 'Yr.no returned an updated forecast for @location', array(
        '@location' => $location->name,
      ), WATCHDOG_DEBUG);
    }
    return $data;
  }
  else {
    watchdog('yr_verdata', 'Yr.no did not return 200 OK, and/or the XML for @location was invalid. URL was !url', array(
      '@location' => $location->name,
      '!url' => $feed,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
}