Advertisement
Guest User

Untitled

a guest
Aug 13th, 2014
3,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // парсер изображений и описания состояний
  3. $col = 1; // кол-во дней (до 10, увеличивается время выдачи)
  4. $data_file = 'http://weather.yandex.ru/static/cities.xml';
  5. $xml = simplexml_load_file($data_file);
  6.  
  7. foreach ($xml->country as $key => $value) {
  8.     foreach ($value->city as $key1 => $value1):
  9.         $out = getWeather($value1["id"], $col);
  10.         foreach ($out as $day):
  11.             foreach ($day['weather'] as $weather):
  12.                 $types[(string)$weather['weather_type']] = (string)$weather['image'];  // массив описаний состояния с изображением
  13.                 $images[(string)$weather['image']] = (string)$weather['weather_type']; // массив изображений с описанием состояния
  14.             endforeach;
  15.         endforeach;
  16.     endforeach;
  17. }
  18. foreach($types as $type=>$img):
  19.     ?><img src="http://yandex.st/weather/1.2.61/i/icons/48x48/<?php echo $img;?>.png" width="48" height="48" /> - <?php echo $type.'<br />';
  20. endforeach;
  21. echo '<br><hr><br>';
  22. foreach($images as $img=>$type):
  23.     ?><img src="http://yandex.st/weather/1.2.61/i/icons/48x48/<?php echo $img;?>.png" width="48" height="48" /> - <?php echo $type.'<br />';
  24. endforeach;
  25. /*
  26. заполняем массив при помощи функции, первый параметр идентификатор города, другие параметры необязательны - в этом случае используется значения по умолчанию
  27. */
  28. function getWeather($city, $col = 10) {
  29.     $data_file = 'http://export.yandex.ru/weather-ng/forecasts/'.$city.'.xml';   // загружаем файл прогноза погоды для выбранного города
  30.     $xml = simplexml_load_file($data_file); // загружаем xml файл через simple_xml
  31.  
  32.     $out = array(); // массив вывода прогноза
  33.     $counter = 0 ; // счетчик количества дней, для которых доступен прогноз
  34.  
  35.     if($xml->day):
  36.         foreach($xml->day as $day):
  37.  
  38.             if($counter == $col) break;
  39.             for ($i=0;$i<=3;$i++) {
  40.                 $out[$counter]['weather'][$i]['image'] = $day->day_part[$i]->{'image-v3'};
  41.                 $out[$counter]['weather'][$i]['weather_type'] = $day->day_part[$i]->weather_type;
  42.             }
  43.             $counter++ ;
  44.         endforeach;
  45.     endif;
  46.     return $out ;
  47. }
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement