PHP Скрипт информера погоды / парсер погоды с Яндекса (Яндекс.Погода)
weather.php - скрипт парсинга погоды с Яндекс.Погода.
x.tpl - Шаблон, используемый для получение необходимого вида информера погоды.
x - количество выводимых дней. Обработка подразумевает замену конструкций вида "{t_4}","{pressure_4}" соответсвтующими актуальными значениями прогноза:
- {date_y} - дата
- {t_x} - температура + часть дня
- {icon_x} - изображение погоды
- {data_x} -
- {pressure_x} - давление
- {moisture_x} - влажность
- {wind_x} - направление ветра
- {wind-icon_x} - ветер
- x - значение в пределах от 1 до 16 (на каждый день по 4 отрезка времени)
- y - от 1 до 3
Скрипт необходимо вызывать следующим способом:
<?php include('weather.php?city=xxxxx&c=y');
?>
city - 5 значный идентификатор города. Надо взять с Яндекс.Погода
c=количесво дней, на которое Вы желаете получить прогноз.
Все данные будут сохранятся в файл вида wth_xxxxx_y.html
Перед использованием рекомендуется изменить следующие переменные: $period=3600; // время обновления
$wherethescript='/img/weather/';
папка с изображениями
Php код скрипта информера погоды:
<?php
if (!isset($_GET['city'])||!isset($_GET['c'])) exit();
$city=substr(ereg_replace('[^0-9]','',$_GET['city']),0,10);
$c=substr(ereg_replace('[^0-9]','',$_GET['c']),0,1);
$filewithweather='wth_'.$city.'_'.$c.'.html';
$period=3600;
$wherethescript='/img/weather/';
$tpl=file_get_contents($c.'.tpl');
if (time()-filemtime($filewithweather)>$period) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://weather.yandex.ru/'.$city.'/details/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$f = mb_convert_encoding(curl_exec($ch),'windows-1251','utf-8');
curl_close($ch);
$f=preg_replace('/weather\/moon\//i','',$f);$f=preg_replace('/weather\/wind\//i','',$f);$f=preg_replace('/http:\/\/img\.yandex\.net\/i\//i',$wherethescript,$f);
$f=str_replace('?','-',$f);
preg_match_all('/<td class="icon">(.*?)<\/td>/i',$f,$weather['icon']);
preg_match_all('/<td class="t">(.*?)<\/td>/i',$f,$weather['t']);
preg_match_all('/<th class="date" rowspan="4">(.*?)<\/th>/i',$f,$weather['date']);
preg_match_all('/<td class="data">(.*?)<\/td>/i',$f,$weather['data']);
preg_match_all('/<td class="pressure">(.*?)<\/td>/i',$f,$weather['pressure']);
preg_match_all('/<td class="moisture">(.*?)<\/td>/i',$f,$weather['moisture']);
preg_match_all('/<td class="wind">(.*?)<\/td>/i',$f,$weather['wind']);
preg_match_all('/<td class="wind-icon">(.*?)<\/td>/i',$f,$weather['wind-icon']);
preg_match_all('/<td class="addon">(.*?)<\/td>/i',$f,$weather['addon']);
$date=(date('G',time())+2);
if (6<=$date&&$date<12) $weather['cur_weather'][1][1]=$weather['t'][1][0];
if (12<=$date&&$date<18) $weather['cur_weather'][1][1]=$weather['t'][1][1];
if (18<=$date&&$date<24) $weather['cur_weather'][1][1]=$weather['t'][1][2];
if (0<=$date&&$date<6) $weather['cur_weather'][1][1]=$weather['t'][1][3];
foreach ($weather as $par=>$val)
{$i=1; foreach ($val[1] as $subval)
$tpl= preg_replace('(\{'.$par.'_'.($i++).'\})',$subval,$tpl,1);
}
fwrite(fopen($filewithweather,'w'),$tpl);
}
if (file_exists($filewithweather)) include($filewithweather);
?>
Пример для одного дня: "Погода во Владимире {t_1} {icon_1}" будет заменен на "Погода во владимире +3 .. +8 утро <значок>"
Пример шаблона информера погоды для 3х дней:
3.tlp:
Давление | Влажность | Скорость и направление ветра м/с | ||||
{date_1}
|
{t_1} | {icon_1} | {pressure_1} | {moisture_1} | {wind_1} | |
{t_2} | {icon_2} | {pressure_2} | {moisture_2} | {wind_2} | ||
{t_3} | {icon_3} | {pressure_3} | {moisture_3} | {wind_3} | ||
{t_4} | {icon_4} | {pressure_4} | {moisture_4} | {wind_4} | ||
Давление | Влажность | Скорость и направление ветра м/с | ||||
{date_2}
|
{t_5} | {icon_5} | {pressure_5} | {moisture_5} | {wind_5} | |
{t_6} | {icon_6} | {pressure_6} | {moisture_6} | {wind_6} | ||
{t_7} | {icon_7} | {pressure_7} | {moisture_7} | {wind_7} | ||
{t_8} | {icon_8} | {pressure_8} | {moisture_8} | {wind_8} | ||
Давление | Влажность | Скорость и направление ветра м/с | ||||
{date_3}
|
{t_9} | {icon_9} | {pressure_9} | {moisture_9} | {wind_9} | |
{t_10} | {icon_10} | {pressure_10} | {moisture_10} | {wind_10} | ||
{t_11} | {icon_11} | {pressure_11} | {moisture_11} | {wind_11} | ||
{t_12} | {icon_12} | {pressure_12} | {moisture_12} | {wind_12} |
Да, яндекс изменил структуру. Парсер на сегодня не рабочий. Как только появиться заказ на яндекс.погоду выложим рабочий скрипт.