Вывести в блоке погоду в Москве на основе данных яндекса

Пример модуля для вывода в блоке текущей погоды в Москве:

Пример блока

/**
 * Implements hook_block_info().
 */
function weather_block_info() {
  return array(
    'weather' => array(
      'info' => 'Погода в Москве',
      'cache' => DRUPAL_NO_CACHE,
    ),
  );
}
 
/**
 * Implements hook_block_view().
 */
function weather_block_view($delta = '') {
  $block = array();
  if ($delta == 'weather') {
    $weather = weather_get_weather();
    $block['subject'] = 'Погода в Москве';
    $block['content'] = '
      <img src="http://img.yandex.net/i/wiz' . $weather['image'] . '.png" alt="' . $weather['type'] . '" />
      ' . ($weather['temperature'] > 0 ? '+' . $weather['temperature'] : $weather['temperature']) . '
    ';
  }
  return $block;
}
 
/**
 * Return weather.
 */
function weather_get_weather($ignore_cache = FALSE) {
  if (!$ignore_cache && ($cache = cache_get('weather'))) {
    $weather = $cache->data;
  }
  else {
    $xml = simplexml_load_file('http://export.yandex.ru/weather-ng/forecasts/27530.xml');
    $weather = array(
      'temperature' => (string)$xml->fact->temperature,
      'image' => (string)$xml->fact->image,
      'type' => (string)$xml->fact->weather_type,
    );
    cache_set('weather', $weather);
  }
 
  return $weather;
}
 
/**
 * Implements hook_cron().
 */
function weather_cron() {
  if (REQUEST_TIME - variable_get('cron_last') > 60*60) {
    weather_get_weather(TRUE);
  }
}

Информация о погоде обновляться каждый час при ближайшем запуске крона.

Чтобы вывести погоду для другого города, достаточно в адресе xml-ки заменить 27530 на id города.

Написанное актуально для
Drupal 7
Теги:
yandex, работа с блоками, разработка
Добавлено: 30 Июля 2018 08:23:39 Добавил: Андрей Ковальчук Нравится 0
Добавить
Комментарии:
Нету комментариев для вывода...