How to use Google Weather forecast API in Php
Do you want to display your city Weather forecast by google api, Yes you can do so by php. Google provides Weather API to use on any website. Here Im showing you an example how to use that in php.
<?php $xml = simplexml_load_file('http://www.google.com/ig/api?weather='Your-City-Name'); $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"); ?> |
By this we can connect google api and that send us detail report about weather forecast of a city in xml format. now we need to go show on our website by the help php .
for showing images
<img src="<?php echo 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather" /> <?php echo $current[0]->temp_c['data'] ?>° C, |
change temp_f['data']Â to display in fahrenheit
<?php echo $current[0]->condition['data'] ?> |
for forecast use these codes
<?php foreach ($forecast_list as $forecast) : ?> <img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather" /> <?php echo $forecast->day_of_week['data']; ?> <?php echo $forecast->low['data'] ?>° F - <?php echo $forecast->high['data'] ?>° F, <?php echo $forecast->condition['data'] ?> <?php endforeach;Â /// end loop ?> |
please check and update me
enjoy











