Skip to content

Instantly share code, notes, and snippets.

@carwin
Created September 6, 2012 01:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carwin/3649766 to your computer and use it in GitHub Desktop.
Save carwin/3649766 to your computer and use it in GitHub Desktop.
PHP: Use Yahoo's Weather API, cURL, and SimpleXML to get current weather conditions given a WOEID.
<?php
/*
* This gist utilizes Yahoo's Weather API to snag the current
* weather conditions for any country given it's WOEID.
*
* - by Carwin Young
*/
/* Set up some location WOEIDs */
$Cambodia = 1020985; /* Phnom Pneh */
$Jakarta = 1047378; /* Jakarta */
$Bali = 1047372; /* Denpasar */
$Russia = 2123260; /* St. Petersburg */
/* Use cURL to query the API for some XML */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://weather.yahooapis.com/forecastrss?w='.$Jakarta.'&u=f');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$weather_rss = curl_exec($ch);
curl_close($ch);
/* Create an object of the XML returned */
$weather = new SimpleXMLElement($weather_rss);
/*
* Since I don't want to figure out an image system, I'll use Weather.com's (what Yahoo does)
* by pulling the image directly out of the returned API request. This could be done better.
*/
$weather_contents = $weather->channel->item->description;
preg_match_all('/<img[^>]+>/i',$weather_contents, $img);
$weather_img = $img[0][0];
/* Get clean parts */
$weather_unit = $weather->channel->xpath('yweather:units');
$weather_cond = $weather->channel->item->xpath('yweather:condition');
$weather_wind = $weather->channel->xpath('yweather:wind');
/* Function to convert Wind Direction from given degree units into Cardinal units */
function cardinalize($degree) {
if($degree == 0) $direction = '';
if($degree >= 348.75 && $degree <= 11.25) $direction = 'N';
if($degree > 11.25 && $degree <= 33.75) $direction = 'NNE';
if($degree > 33.75 && $degree <= 56.25) $direction = 'NE';
if($degree > 56.25 && $degree <= 78.75) $direction = 'ENE';
if($degree > 78.75 && $degree <= 101.25) $direction = 'E';
if($degree > 101.25 && $degree <= 123.75) $direction = 'ESE';
if($degree > 123.75 && $degree <= 146.25) $direction = 'SE';
if($degree > 146.25 && $degree <= 168.75) $direction = 'SSE';
if($degree > 168.75 && $degree <= 191.25) $direction = 'S';
if($degree > 191.25 && $degree <= 213.75) $direction = 'SSW';
if($degree > 213.75 && $degree <= 236.25) $direction = 'SW';
if($degree > 236.25 && $degree <= 258.75) $direction = 'WSW';
if($degree > 258.75 && $degree <= 281.25) $direction = 'W';
if($degree > 281.25 && $degree <= 303.75) $direction = 'WNW';
if($degree > 303.75 && $degree <= 326.25) $direction = 'NW';
if($degree > 326.25 && $degree < 348.75) $direction = 'NNW';
return $direction;
}
?>
<style>
.left{float: left;overflow:hidden;}.right{float:right;}
.weather_block{overflow:hidden;width: 220px;font-family: Arial, sans-serif; color: #6a6a6a;}
.temp{font-weight:bold;font-size:17px;margin-right: 10px;}
.wcond{font-size:11px;}
</style>
<div class="weather_block">
<div class="left">
<?php print $weather_img; ?>
</div>
<div class="left">
<div class="temp left">
NOW<br /><?php print $weather_cond[0]->attributes()->temp; ?>&deg; <?php print $weather_unit[0]->attributes()->temperature; ?>
</div>
<div class="right">
<div class="wcond">
<?php print $weather_cond[0]->attributes()->text; ?><br /><br />
Wind: <?php print cardinalize($weather_wind[0]->attributes()->direction); ?>
<?php
if($weather_wind[0]->attributes()->speed != 0){
print $weather_wind[0]->attributes()->speed.' '.$weather_unit[0]->attributes()->speed;
}else{
print 'none';
}
?>
</div>
</div>
</div>
</div>
<?php
function get_weather() {
/* Set up some location WOEIDs */
$cambodia = 1020985; /* Phnom Pneh */
$jakarta = 1047378; /* Jakarta */
$bali = 1047372; /* Denpasar */
$russia = 2123260; /* St. Petersburg */
$south_korea = 1132599; /* Seoul */
/* Check the current URL, grab a Place, check it against set up WOEIDs, assign it */
$url = $_SERVER['REQUEST_URI']; // e.g.: destination/jakarta
$url_parts = explode('/', $url); // $url_parts = array( [0] => destination, [1] => jakarta )
$url_place = $url_parts[2]; // $url_place = jakarta
if($url_place == 'cambodia') $woeid = $cambodia;
if($url_place == 'jakarta') $woeid = $jakarta;
if($url_place == 'bali') $woeid = $bali;
if($url_place == 'russia') $woeid = $russia;
if($url_place == 'south-korea') $woeid = $south_korea;
/* Use cURL to query the API for some XML */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://weather.yahooapis.com/forecastrss?w='.$woeid.'&u=f');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$weather_rss = curl_exec($ch);
curl_close($ch);
/* Create an object of the XML returned */
$weather = new SimpleXMLElement($weather_rss);
/*
* Since I don't want to figure out an image system, I'll use Weather.com's (what Yahoo does)
* by pulling the image directly out of the returned API request. This could be done better.
*/
$weather_contents = $weather->channel->item->description;
preg_match_all('/<img[^>]+>/i',$weather_contents, $img);
$weather_img = $img[0][0];
/* Get cleaner parts */
$weather_unit = $weather->channel->xpath('yweather:units');
$weather_cond = $weather->channel->item->xpath('yweather:condition');
$weather_wind = $weather->channel->xpath('yweather:wind');
/* Make this function spit out some HTML */
$output = '<div class="weather_block"><div class="left">';
$output .= $weather_img;
$output .= '</div><div class="left"><div class="temp left">Now<br />';
$output .= $weather_cond[0]->attributes()->temp.'&deg;'.$weather_unit[0]->attributes()->temperature;
$output .= '</div><div class="left" style="padding-top: 10px;"><div class="wcond">';
$output .= $weather_cond[0]->attributes()->text;
$output .= '<br />Wind: ';
$output .= cardinalize($weather_wind[0]->attributes()->direction);
if($weather_wind[0]->attributes()->speed != 0) {
$output .= ' '.$weather_wind[0]->attributes()->speed.$weather_unit[0]->attributes()->speed;
}else{
$output .= ' none';
}
$output .= '</div></div></div></div>';
return $output;
}
/* Function to convert Wind Direction from given degree units into Cardinal units */
function cardinalize($degree) {
if($degree == 0) $direction = '';
if($degree >= 348.75 && $degree <= 11.25) $direction = 'N';
if($degree > 11.25 && $degree <= 33.75) $direction = 'NNE';
if($degree > 33.75 && $degree <= 56.25) $direction = 'NE';
if($degree > 56.25 && $degree <= 78.75) $direction = 'ENE';
if($degree > 78.75 && $degree <= 101.25) $direction = 'E';
if($degree > 101.25 && $degree <= 123.75) $direction = 'ESE';
if($degree > 123.75 && $degree <= 146.25) $direction = 'SE';
if($degree > 146.25 && $degree <= 168.75) $direction = 'SSE';
if($degree > 168.75 && $degree <= 191.25) $direction = 'S';
if($degree > 191.25 && $degree <= 213.75) $direction = 'SSW';
if($degree > 213.75 && $degree <= 236.25) $direction = 'SW';
if($degree > 236.25 && $degree <= 258.75) $direction = 'WSW';
if($degree > 258.75 && $degree <= 281.25) $direction = 'W';
if($degree > 281.25 && $degree <= 303.75) $direction = 'WNW';
if($degree > 303.75 && $degree <= 326.25) $direction = 'NW';
if($degree > 326.25 && $degree < 348.75) $direction = 'NNW';
return $direction;
}
?>
@scuba323
Copy link

Would this be OK to use on my website to tie into an Android app? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment