Created
February 26, 2010 16:34
-
-
Save jeremeamia/315880 to your computer and use it in GitHub Desktop.
Get weather w/o API keys
This file contains hidden or 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 | |
/** | |
* Small, free, weather web-service using Yahoo APIs that don't require an API key. | |
*/ | |
// Fetch zip code | |
$location = isset($_GET['location']) ? $_GET['location'] : 'Tempe,AZ'; | |
// Use YQL to get the WOEID by the location | |
$query = 'select * from geo.places where text="'.$location.'"'; | |
$yql = simplexml_load_file('http://query.yahooapis.com/v1/public/yql?q='.urlencode($query).'&format=xml'); | |
$woeid = strval($yql->results->place[0]->woeid); | |
// Use Yahoo weather RSS to get the weather by the WOEID | |
$feed = simplexml_load_file('http://weather.yahooapis.com/forecastrss?w='.$woeid); | |
$location = current($feed->xpath('//yweather:location'))->attributes(); | |
$weather = current($feed->xpath('//yweather:condition'))->attributes(); | |
preg_match('/<img src="([^"]+)"/i', strval($feed->channel->item[0]->description), $image); | |
// Display the weather | |
echo '<p><img src="'.$image[1].'" />'.$weather['temp'].'°F in '.$location['city'].', '.$location['region'].', '.$location['country'].'</p>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment