Wednesday, September 7, 2011

Google Weather API Widget (PHP) (No longer Working)




Note: Google has killed the free hidden weather API so this method no longer works

The Google Weather API is a hidden service for developers to get weather data for any location with ease.
Indeed, you just simply pass a city name or postal code, such as this Mountain View

Query by City
http://www.google.com/ig/api?weather=Mountain+View   

Query by Coordinates
http://www.google.com/ig/api?weather=,,,30670000,104019996 

To get an XML response like this:


This is the php code to get a widget like shown above. Line 20 defines the location for the widget.
Either you can use postal code, city or coordinates multiplied by 1000000 (Check Line No 21,22,23,25)

1:  <?php  
2:  function convert($temp)  
3:  {  
4:    // Converting Fahrenheit To Celsius, vice versa  
5:    global $config;  
6:    $temperature = $temp;  
7:    if( strtoupper($config['base-temp-unit']) == 'F' && strtoupper($config['display-temp-unit']) == 'C' )  
8:    {  
9:      // Converting Fahrenheit To Celsius  
10:      $temperature = round((5/9)*($temp-32));  
11:    }  
12:    if( strtoupper($config['base-temp-unit']) == 'C' && strtoupper($config['display-temp-unit']) == 'F' )  
13:    {  
14:      // Converting Celsius to Fahrenheit  
15:      $temperature = round((9/5)*$temp+32);  
16:    }  
17:    return $temperature;  
18:  }  
19:  $url = "http://www.google.com";  
20:  $location = "colombo"; // <city>,<country code>  
21:  $lat;  
22:  $lng;  
23:  //$location = ",,,30670000,104019996"; // Coordinates
24:  $weather_url = "{$url}/ig/api?weather={$location}";  
25:  //$weather_url = "{$url}/ig/api?weather=,,,{$lat},{$lng}";  
26:  $config['base-temp-unit'] = 'F'; // F=Fahrenheit, C=Celsius  
27:  $config['display-temp-unit'] = 'C'; // F=Fahrenheit, C=Celsius  
28:  if( $xmlData = file_get_contents($weather_url) )  
29:  {  
30:    $xml = new SimpleXMLElement($xmlData);  
31:    $eol = "\r\n";  
32:    // Display basic information  
33:       echo("<table width='70%' border='0' cellspacing='0' cellpadding='5'>");  
34:       echo("<tr>");  
35:       echo("<td width='11%' rowspan='4' align='center' valign='middle'>");  
36:       echo("<img src='{$url}{$xml->weather->current_conditions->icon->attributes()}' alt='' width='60' height='60' border='0' style='margin-right: 3px; vertical-align: top;>'");  
37:       echo("</td>");  
38:       echo("<td width='33%' align='left' valign='top'>{$xml->weather->current_conditions->temp_c->attributes()} C</td>");  
39:       echo("<td width='56%' rowspan='4' align='left' valign='top'><table width='100%' border='0' cellspacing='0' cellpadding='5' style='border-left:1px solid #CCCCCC'>");  
40:       echo("<tr>");  
41:       foreach( $xml->weather->forecast_conditions as $i => $result )  
42:    {  
43:       echo("<td align='center'>{$result->day_of_week->attributes()}</td>");  
44:       }  
45:       echo("</tr><tr>");  
46:       foreach( $xml->weather->forecast_conditions as $i => $result )  
47:    {  
48:       echo("<td align='center'><img src='{$url}{$result->icon->attributes()}'></td>");  
49:       }  
50:       echo("</tr><tr>");  
51:       foreach( $xml->weather->forecast_conditions as $i => $result )  
52:    {  
53:       echo("<td align='center'><table width='100%' border='0' cellspacing='0' cellpadding='5'><tr><td align='center'>".convert($result->high->attributes())." ".strtoupper($config['display-temp-unit'])."</td><td align='center'>".convert($result->low->attributes())." ".strtoupper($config['display-temp-unit'])."</td></tr></table></td>");  
54:       }  
55:       echo("</tr></table></td></tr><tr>");  
56:       echo("<td>{$xml->weather->current_conditions->condition->attributes()}</td>");  
57:       echo("</tr><tr>");  
58:       echo("<td>{$xml->weather->current_conditions->wind_condition->attributes()}</td>");  
59:       echo("</tr><tr>");  
60:       echo("<td>{$xml->weather->current_conditions->humidity->attributes()}</td>");  
61:       echo("</tr></table>");  
62:  }  
63:  ?>  

5 comments:

  1. Hi,
    The weather google api cannot be used. They treat thee request to get the xml file as an automated query, and just outputs this message:
    We’re sorry… … but your computer or network may be sending automated queries. To protect our users, we can’t process your request right now.

    ReplyDelete
    Replies
    1. Looks like Google has killed the free hidden API, So this method no longer works

      Source http://thenextweb.com/google/2012/08/28/did-google-just-quietly-kill-private-weather-api/

      Delete
  2. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
    Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

    ReplyDelete
  3. I am really thankful for posting such useful information. It really made me understand lot of important concepts in the topic. Keep up the good work!
    Oracle Training in Chennai | Oracle Course in Chennai

    ReplyDelete
  4. Infycle Technologies, the No.1 software training institute in Chennai offers the No.1 Selenium course in Chennai for tech professionals, freshers, and students at the best offers. In addition to the Selenium, other in-demand courses such as Python, Big Data, Oracle, Java, Python, Power BI, Digital Marketing, Cyber Security also will be trained with hands-on practical classes. After the completion of training, the trainees will be sent for placement interviews in the top companies. Call 7504633633 to get more info and a free demo.

    ReplyDelete