-
-
Save xom9k/35758e07e0b5bd736692 to your computer and use it in GitHub Desktop.
Yandex.Weather Snippet for MODX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[!weather? | |
&cityId=`26850` | |
&tpl=`weather.tpl` | |
&cacheTime=`7200` | |
]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 файла | |
$key = 'weather_' . $cityId; | |
if (!$output = $modx->cacheManager->get($key)) { | |
$status = get_headers($source); //проверка доступности сервера погоды | |
if (!in_array("HTTP/1.1 200 OK", $status) | |
&& !in_array("HTTP/1.0 200 OK", $status)) { | |
return 'Сервер погоды недоступен'; | |
} | |
$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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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