Google Weather API + PHP
El Tiempo en tu Web con PHP
Si quieres mostrar el tiempo de una ciudad / país en tu página web, puedes usar la API de Google Weather (Tiempo) que es facil de configurar y cambiar para ajustarlo a tu localización. Puedes realizar consultas sobre la URI de Google con el siguiente formato:
http://www.google.com/ig/api?weather = [nombre de la ciudad]
Por ejemplo, solicitando la siguiente URI:
http://www.google.com/ig/api?weather=barcelona
Esta consulta retornará un archivo XML que se puede procesar facilmente con PHP con el siguiente código:
| <? $xml = simplexml_load_file(‘http://www.google.com/ig/api?weather=barcelona’); $information = $xml->xpath(“/xml_api_reply/weather/forecast_information”); $current = $xml->xpath(“/xml_api_reply/weather/current_conditions”); $forecast_list = $xml->xpath(“/xml_api_reply/weather/forecast_conditions”); ?> <html> <head> <title>Google Weather API</title> </head> <body> <h1><?= print $information[0]->city['data']; ?></h1> <h2>Hoy:</h2> <div class=”weather”> <img src=”<?= ‘http://www.google.com’ . $current[0]->icon['data']?>” alt=”weather”?> <span class=”condition”> <?= $current[0]->temp_f['data'] ?>° F, <?= $current[0]->condition['data'] ?> </span> </div> <h2>Prevision:</h2> <? foreach ($forecast_list as $forecast) : ?> <div class=”weather”> <img src=”<?= ‘http://www.google.com’ . $forecast->icon['data']?>” alt=”weather”?> <div><?= $forecast->day_of_week['data']; ?></div> <span class=”condition”> <?= $forecast->low['data'] ?>° F – <?= $forecast->high['data'] ?>° F, <?= $forecast->condition['data'] ?> </span> </div> <? endforeach ?> </body> </html> |
El resultado del código és el siguiente:
Esperamos que os sirva de ayuda.
Comments Off


