Parsing DOM with PHP
From Leo's Notes
This page was last edited on 25 May 2017, at 23:55.
$data = file_get_contents("http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/AB/s0000047_e.xml");
$weather = new SimpleXMLElement($data);
$weather_data->location = (string) $weather->location->name;
$weather_data->date = (string) $weather->dateTime[1]->textSummary;
$weather_data->text = (string) $weather->currentConditions->condition;
$weather_data->forecastAbbreviated = (string) $weather->forecastGroup->forecast->abbreviatedForecast->textSummary;
$weather_data->forecast = (string) $weather->forecastGroup->forecast->textSummary;
$weather_data->pressure = (string) $weather->currentConditions->pressure;
$weather_data->temp = (string) $weather->currentConditions->temperature;
$forecasts = array();
foreach($weather->forecastGroup->forecast as $forecast) {
$forecasts[] = array(
"period" => (string)$forecast->period,
"forecast" => (string)$forecast->textSummary,
"text" => (string)$forecast->abbreviatedForecast->textSummary,
"pop" => (int)$forecast->abbreviatedForecast->pop,
"temperature" => (int)$forecast->temperatures->temperature,
"windchill" => (int)$forecast->windChill->calculated,
"icon" => (string)$forecast->abbreviatedForecast->iconCode
);
}
$weather_data->forecasts = $forecasts;
$icon = "http://weather.gc.ca/weathericons/" . $weather->currentConditions->iconCode . ".gif";
$weather_data->icon = $icon;
$weather_data->source = "Environment Canada";