Parsing DOM with PHP

From Leo's Notes
Revision as of 23:55, 25 May 2017 by Leo (talk | contribs) (Created page with " {{highlight|lang=php|code= $data = file_get_contents("http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/AB/s0000047_e.xml"); $weather = new SimpleXMLElement($data); $we...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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";