/2008/02/yahoo-weather.png)
xml.weather.yahoo.com/forecastrss?p=_&u=_YahooWeather class (PHP5) described below is designed to download RSS over HTTP and preprocess data. <? php
class YahooWeather {
// Wind
public $ wind_chill;
public $ wind_direction;
public $ wind_speed;
// Atmospheric indicators
public $ humidity;
public $ visibility;
public $ pressure;
// We translate sunrise and sunset time to unix time
public $ sunrise;
public $ sunset;
// Current air temperature and weather
public $ temp;
public $ condition_text;
public $ condition_code;
// Weather forecast for 5 days
public $ forecast;
public $ units;
function __construct ($ code, $ units = 'c', $ lang = 'en') {
$ this-> units = ($ units == 'c')? 'c': 'f';
$ url = 'http://xml.weather.yahoo.com/forecastrss?p='.
$ code. '& u ='. $ this-> units;
$ xml_contents = file_get_contents ($ url);
if ($ xml_contents === false)
throw new Exception ('Error loading'. $ url);
$ xml = new SimpleXMLElement ($ xml_contents);
// Wind
$ tmp = $ xml-> xpath ('/ rss / channel / yweather: wind');
if ($ tmp === false) throw new Exception ("Error parsing XML.");
$ tmp = $ tmp [0];
$ this-> wind_chill = (int) $ tmp ['chill'];
$ this-> wind_direction = (int) $ tmp ['direction'];
$ this-> wind_speed = (int) $ tmp ['speed'];
// Atmospheric indicators
$ tmp = $ xml-> xpath ('/ rss / channel / yweather: atmosphere');
if ($ tmp === false) throw new Exception ("Error parsing XML.");
$ tmp = $ tmp [0];
$ this-> humidity = (int) $ tmp ['humidity'];
$ this-> visibility = (int) $ tmp ['visibility'];
$ this-> pressure = (int) $ tmp ['pressure'];
// We translate sunrise and sunset time to unix time
$ tmp = $ xml-> xpath ('/ rss / channel / yweather: astronomy');
if ($ tmp === false) throw new Exception ("Error parsing XML.");
$ tmp = $ tmp [0];
$ this-> sunrise = strtotime ($ tmp ['sunrise']);
$ this-> sunset = strtotime ($ tmp ['sunset']);
// Current air temperature and weather
$ tmp = $ xml-> xpath ('/ rss / channel / item / yweather: condition');
if ($ tmp === false) throw new Exception ("Error parsing XML.");
$ tmp = $ tmp [0];
$ this-> temp = (int) $ tmp ['temp'];
$ this-> condition_text = strtolower ((string) $ tmp ['text']);
$ this-> condition_code = (int) $ tmp ['code'];
// Weather forecast for 5 days
$ forecast = array ();
$ tmp = $ xml-> xpath ('/ rss / channel / item / yweather: forecast');
if ($ tmp === false) throw new Exception ("Error parsing XML.");
foreach ($ tmp as $ day) {
$ this-> forecast [] = array (
'date' => strtotime ((string) $ day ['date']),
'low' => (int) $ day ['low'],
'high' => (int) $ day ['high'],
'text' => (string) $ day ['text'],
'code' => (int) $ day ['code']
);
}
}
public function __toString () {
$ u = "°". (($ this-> units == 'c')? 'C': 'F');
return $ this-> temp. ' '. $ u.', '. $ this-> condition_text;
}
}
?> <? php
try {
$ weather = new YahooWeather ('RSXX0091');
} catch (Exception $ e) {
echo "Caught exception:". $ e-> getMessage ();
exit ();
}
echo '<h1>'. $ weather. '</ h1>';
echo "<pre>";
print_r ($ weather);
echo "</ pre>";
?> /2008/02/weather-report.png)
condition_code field is used to store the weather code code. The text decoding of the current value is stored in the condition_text field, and the full list of possible options is given in the following table:| Code | Description |
| 0 | tornado |
| one | tropical storm |
| 2 | hurricane |
| 3 | severe thunderstorms |
| four | thunderstorms |
| five | mixed rain and snow |
| 6 | mixed rain and sleet |
| 7 | mixed snow and sleet |
| eight | freezing drizzle |
| 9 | drizzle |
| ten | freezing rain |
| eleven | showers |
| 12 | showers |
| 13 | snow flurries |
| 14 | light snow showers |
| 15 | blowing snow |
| sixteen | snow |
| 17 | hail |
| 18 | sleet |
| nineteen | dust |
| 20 | foggy |
| 21 | haze |
| 22 | smoky |
| 23 | blustery |
| 24 | windy |
| 25 | cold |
| 26 | cloudy |
| 27 | mostly cloudy (night) |
| 28 | mostly cloudy (day) |
| 29 | partly cloudy (night) |
| thirty | partly cloudy (day) |
| 31 | clear (night) |
| 32 | sunny |
| 33 | fair (night) |
| 34 | fair (day) |
| 35 | mixed rain and hail |
| 36 | hot |
| 37 | isolated thunderstorms |
| 38 | scattered thunderstorms |
| 39 | scattered thunderstorms |
| 40 | scattered showers |
| 41 | heavy snow |
| 42 | scattered snow showers |
| 43 | heavy snow |
| 44 | partly cloudy |
| 45 | thundershowers |
| 46 | snow showers |
| 47 | isolated thundershowers |
| 3200 | not available |
Source: https://habr.com/ru/post/20461/
All Articles