How to add google weather forecast in php

5.00 Rating, Out of: 5 Votes: 11 Star2 Stars3 Stars4 Stars5 Stars
Loading ... Loading ...

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-&gt;xpath("/xml_api_reply/weather/forecast_information");
$current = $xml-&gt;xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml-&gt;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'] ?>&deg; 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'] ?>&deg; F - <?php echo $forecast->high['data'] ?>&deg; F,
	            <?php echo $forecast->condition['data'] ?>
 
<?php endforeach;  /// end loop
?>

please check and update me

enjoy