Sunday, May 24, 2015

Getting Live Environment Canada Weather Using PHP

The other day a little project I had running at home required some live weather data... Nothing fancy, just the current temptation, condition and a quick blurb for the forecast.

I had a number of ways I could've done this, but I chose to do this in PHP (for me, it was just a process that would run and put these three items somewhere else, so nothing major necessary).

The PHP code is here, just in case anyone else needs to do this.

<?php

$weatherXML = getLiveWeatherXML();

$toronto = simplexml_load_string($weatherXML);
$currentCondition = $toronto->currentConditions->condition;
$currentTemperature = $toronto->currentConditions->temperature;
$forecastCondition = $toronto->forecastGroup->forecast->textSummary;

$arrayData = array(
array('CURRCONDITON' => $currentCondition),
array('CURRTEMP' => $currentTemperature),
array('FORECAST' => $forecastCondition),
);

//Do what you want with the array here...


function getLiveWeatherXML() {
return file_get_contents("http://dd.weather.gc.ca/citypage_weather/xml/ON/s0000458_e.xml");
}

?>