How to add the weather to your website
Everybody likes the weather. Just in case if your readers could possibly care about the climate in the their city or other areas they might be planning to travel. Therefore, I have come up with a code snippet using PHP with Google Secret Weather API to pull out the current weather conditions and their forecast which you can easily display in your sidebar, header or even footer of your website.
Let’s start with writing a function to get the weather and it’s attributes. We will use SimpleXML to load the xml file generated by Google’s Secret API for Weather in a variable. SimpleXML is a PHP extension which provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.
We then will traverse the each document node containing specific attributes (current weather, minimum and maximum forecast and weather condition) and storing their respective values into an array. Along with this, I have also written a small function to convert Fahrenheit into Celsius in case you need a change. You can also replace the text of temp_c to temp_f if you need everything Fahrenheit.
function Fahrenheit($celcius)
{
$converted = ($celcius - 32) * 0.55;
return round($converted);
}
function ShortWeather($city)
{
$count = 0;
$document = simplexml_load_file("http://www.google.com/ig/api?weather=".$city);
foreach($document->children() as $grandparent)
{
foreach($grandparent->children() as $parent)
{
if($count == 0)
{
if($parent->getName() == 'current_conditions')
{
foreach($parent->children() as $children)
{
if($children->getName() == 'condition')
{
$weatherValues[0] = $children->attributes();
}
if($children->getName() == 'temp_c')
{
$weatherValues[1] = $children->attributes();
}
}
}
if($parent->getName() == 'forecast_conditions')
{
foreach($parent->children() as $children)
{
if($children->getName() == 'low')
{
$low = $children->attributes();
$weatherValues[2] = Fahrenheit($low);
}
if($children->getName() == 'high')
{
$high = $children->attributes();
$weatherValues[3] = Fahrenheit($high);
}
}
$count = $count + 1;
}
}
}
}
return $weatherValues;
}
So when you’re done with the functions, you can call them in a same or different files as required. For the simplicity, I have called it in the same file.
$weatherValues;
$NewYork[] = ShortWeather("New York");
$London[] = ShortWeather("London");
$HongKong[] = ShortWeather("Hong Kong");
$Sydney[] = ShortWeather("Sydney");
You can then easily display the extracted values in HTML table or in any div etcetera.
<table border="0" cellspacing="0" cellpadding="3" width="500">
<tr>
<td style="height: 27px">New York</td>
<td style="text-align: center; height: 27px;"><?php echo $NewYork[0][0]; ?></td>
<td style="text-align: center; height: 27px;"><?php echo $NewYork[0][1]; ?></td>
<td style="text-align: center; height: 27px;"><?php echo $NewYork[0][2]; ?></td>
<td style="text-align: center; height: 27px;"><?php echo $NewYork[0][3]; ?></td>
</tr>
</table>
That is it. You can now, just display the weather of your desired locations anywhere on your website/blog.
In my next post, I will let you know guys how can you add a geo-location based weather for your readers. Happy Coding!
| Print article | This entry was posted by geek on February 10, 2011 at 8:10 pm, and is filed under Development, How To. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |


