-
-
Save ryanlund/11396386 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//assumes file already downloaded! | |
$local_file='feed.xml'; | |
$result = file_get_contents($local_file); | |
$xml = simplexml_load_string($result); | |
$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0'); | |
$location = $xml->channel->xpath('yweather:location'); | |
//Should only be one location? Change if necessary.. | |
//check ALL data that you're going to use | |
if(is_array($location)&&count($location)==1&&isset($location[0]['city'][0])) { | |
//original loop not needed, all forecast data stored in one array | |
$forecast = $xml->channel->item->xpath('yweather:forecast'); | |
if(is_array($forecast)&&count($forecast)>0){ //check forecast data exists! | |
$projection=array(); //always initialize your variables. | |
foreach($forecast as $day) { | |
$projection[] = array( | |
'day' => (string)$day['day'], | |
'low' => (int) round($day['low'],0), //round to int just to be sure | |
'high' => (int) round($day['high'],0), | |
'code' => (int) $day['code'] | |
); | |
} | |
$weather = array( | |
'location' => $location[0]['city'][0], | |
'forecast' => $projection | |
); | |
if(isset($_SERVER['argv'])){ | |
print_r($weather); | |
} | |
else{ | |
echo '<pre>'.print_r($weather,1).'</pre>'; | |
} | |
} | |
else{ | |
echo "Sorry, there was an error while loading this data. Never mind :\ "; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment