Posted By

mikael12 on 10/03/10


Tagged

google-weather


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

bobbym245


Google Weather


 / Published in: PHP
 

URL: http://blog.programmableweb.com/2010/02/08/googles-secret-weather-api/

Use param hl for localization

  1. /*
  2.  * @param string $destination Example: Paris
  3.  *
  4.  */
  5.  
  6. function get_weather($destination)
  7. {
  8. $ch = curl_init();
  9. $result = '';
  10.  
  11. curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/ig/api?weather='.$destination.'&hl=sk');
  12. curl_setopt($ch, CURLOPT_HEADER, 0);
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL
  14. $res = str_replace('¹', 'Š', utf8_encode(curl_exec($ch)));
  15.  
  16. if ($res === false)
  17. {
  18. echo 'Nepodarilo sa zistiť po�asie.';
  19. return false;
  20. }
  21.  
  22. $xml = new SimplexmlElement($res);
  23.  
  24. if (! empty($xml->weather->current_conditions))
  25. {
  26. foreach($xml->weather as $item) {
  27.  
  28. /* $current = $item->current_conditions;
  29. $result = '<div class="weather_icon">Dnes <br />'.
  30. '<img src="http://www.google.com/' .$current->icon['data'] . '"/><br />'.
  31. $current->temp_c['data'].'&deg;C</div>'; */
  32.  
  33. foreach($item->forecast_conditions as $new) {
  34. $result .= '<div class="weather_icon">'.ucfirst($new->day_of_week['data']).'<br />
  35. <img src="http://www.google.com/' .$new->icon['data'] . '"/><br />'.
  36. $new->low['data'].'-'.$new->high['data'].'&deg;C</div>';
  37.  
  38. }
  39. }
  40. }
  41.  
  42. return $result;
  43.  
  44. }

Report this snippet  

You need to login to post a comment.