Skip to content

Instantly share code, notes, and snippets.

@jeremeamia
Created February 26, 2010 16:34
Show Gist options
  • Save jeremeamia/315880 to your computer and use it in GitHub Desktop.
Save jeremeamia/315880 to your computer and use it in GitHub Desktop.
Get weather w/o API keys
<?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'].'&deg;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