Google weather API with PHP

Hi Guys,

I thought I should share goodies like this with you. There are many a times when we are doing a website for our client and they want to display local weather in the header of their website.

That’s cool thought but how do we do it in PHP.

Its really simple stuff.

Let me show you how

Google weather API can be accessed from this URL

http://www.google.com/ig/api

This is good, the only param you will need is called weather so the above URL will become

http://www.google.com/ig/api?weather=Melbourne

Try to browse above URL and you will get something like this

Google Weather API example

 

 

Alright that looks cool now lets get some key values from the above XML using PHP

with PHP you’ll do something like this

  1. $xml = simplexml_load_file('http://www.google.com/ig/api?weather=Melbourne');
  2. if(isset($information[0]->condition)) {
  3. // get current condition
  4. echo($information[0]->condition->attributes())."<br>";
  5. // get current temperature in Fahrenheit
  6. echo($information[0]->temp_f->attributes())."<br>";
  7.  
  8. // get current temperature in Celsius
  9. echo($information[0]->temp_c->attributes())."<br>";
  10. // get current temperature in Celsius
  11. echo("<img src='$information[0]->icon->attributes()'>")."<br>";
  12. }
  13. else {
  14. echo "Invalid address";
  15. }

As you can see that its pretty easy to get that sort of info.

I hope this helps in some way or another

Cheers

Advertisement

 

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Speak Your Mind

*

CommentLuv badge