Skip to content

Instantly share code, notes, and snippets.

@vgrish
Forked from alroniks/template.html
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrish/bf20095ee01ea1caff0b to your computer and use it in GitHub Desktop.
Save vgrish/bf20095ee01ea1caff0b to your computer and use it in GitHub Desktop.
[[!weather?
&cityId=`26850`
&tpl=`weather.tpl`
&cacheTime=`7200`
]]
<?php
$cityId = $modx->getOption('cityId', $scriptProperties, 26850); // id города https://pogoda.yandex.ru/static/cities.xml
$tpl = $modx->getOption('tpl', $scriptProperties, 'tpl.Weather');
$cacheTime = $modx->getOption('cacheTime', $scriptProperties, 60 * 60); // 1 час по умолчанию
$source = "http://export.yandex.ru/weather-ng/forecasts/$cityId.xml"; // адрес xml файла
$status = get_headers($source); //проверка доступности сервера погоды
if (!in_array("HTTP/1.1 200 OK", $status)
&& !in_array("HTTP/1.0 200 OK", $status)) {
return 'Сервер погоды недоступен';
}
$key = 'weather_' . $cityId;
if (!$output = $modx->cacheManager->get($key)) {
$xml = simplexml_load_file($source); // раскладываем xml на массив
$temperature = (int) $xml->fact->temperature;
$weather = array(
'city' => (string) $xml->fact->station,
'temperature' => $temperature <= 0 ? $temperature : '+' . $temperature, // для наглядности добавляем "+"
'picture' => (string) $xml->fact->image,
'type' => (string) $xml->fact->weather_type
);
$output = $modx->getChunk($tpl, $weather); // Шаблонизация
$modx->cacheManager->set($key, $output, $cacheTime);
}
return $output;
<img src="//img.yandex.net/i/wiz[[+picture]].png" title="[[+type]]">[[+temperature]]<sup>o</sup>C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment