Using Google's Weather API - may be someone needs it.

Yii is Easy and Extensible :)


Hope it helps some one. Attached are the GIFs.

I have removed the form to keep the code clean

Widget Code: /protected/components/weather/GoogleWeatherAPI.php



<?php


class GoogleWeatherAPI extends CWidget


{





    // URL of Google's weather API


    public $API_url = "http://www.google.com/ig/api?";





    // Location of weather inquired, i/p format {zip-code, [City,State]} 


    public $location  = 63069; // can also be: Denver,CO   





    // Deafult o/p set to empty array


    public $weather = array();





    public function init() {


        $api   = $this->API_url.'weather='.$this->location;


        $xml   = simplexml_load_file($api);


        $this->weather = $this->xml2assoc($xml);


    }





    private function xml2assoc($xml) {


        $i = 0;


        foreach ($xml as $e) {


            foreach ($e as $name => $data) {


                if (($name == 'forecast_information') || ($name == 'current_conditions')) {


                    foreach ($data as $col => $val) {


                        $weather[$name][$col] = (string)$val['data'];


                    }


                }


                else {


                    foreach ($data as $col => $val) {


                        $weather['forecast'][$i][$col] = (string)$val['data'];


                    }


                    $i++;


                }


            }


        }


        return $weather;


    }








    public function run() {


        $this->render('google_weather', array('weather'=>$this->weather));


    }





}


?>


Widget's View: /protected/components/weather/views/google_weather.php



<?php


if (!isset($weather[forecast])):


  $weather[forecast]=array();


  echo "check the location - error";


endif;


?>


<?php


// Form to change location can be imbeded here


?>


<table summary='t1'>


  <tr>


    <td class="l">


      <h6>Forcast Information</h6> <hr class="cg" />


      City :<?php echo $weather[forecast_information][city]?> <br />


      Postal code :<?php echo $weather[forecast_information][postal_code]?> <br />


      latitude :<?php echo $weather[forecast_information][latitude_e6]?> <br />


      longitude  :<?php echo $weather[forecast_information][longitude_e6]?> <br />


      forecast date :<?php echo $weather[forecast_information][forecast_date]?> <br />


      current time :<?php echo $weather[forecast_information][current_date_time]; 


       // Do not know who to adjust to correct time zone


      ?><br />


      unit system :<?php echo $weather[forecast_information][unit_system]?> <br />


    </td>


    <td class="l">


      <h6>Current Condition</h6> <hr class="cg" />


        <?php echo $weather[current_conditions][temp_c]?> ºC / <?php $weather[current_conditions][temp_f]?> ºF<br />


        <?php echo $weather[current_conditions][humidity]?> <br />


        <?php echo $weather[current_conditions][wind_condition]?> <br /><br />


        <?php echo gfImgHC ($weather[current_conditions][icon])?> <br />


        <?php echo $weather[current_conditions][condition]?> <br />


    </td>


  </tr>


</table><br /><br />





<table summary='t2'>


  <tr>


    <?php foreach ( $weather[forecast] as $i => $forecast) { ?>


    <td class="l">


      <h6><?php echo $forecast[day_of_week]?></h6> <hr class="cg" />


        low :<?php echo $forecast[low]?> ºF <br />


        high :<?php echo $forecast[high]?> ºF <br /><br />


        <?php echo gfImgHC ($forecast[icon])?> <br />


        <?php echo $forecast[condition]?> <br />


    </td>


    <? } ?>


  </tr>


</table>


Using it in the View: /protected/views/site/weather.php



  <table summary='dd'>


    <tr>


      <td><?php $this->widget('application.components.weather.GoogleWeatherAPI'


                              , array('location'=>$location));


          ?></td>


    </tr>


  </table>


The Global Function in /protected/gfunctions.php



<?php


function gfImgHC ($image, $title=''){ 


  return CHtml::image( 'http://localhost/images/'.$image, $alt='', array('title'=>$title));


}


?>


Hello,

there are some typo in your texte

public $location  = 63069; // can also be: Denver,CO 

Widget’s View: /protected/components/weather/views/google_weather.php

Using it in the View: /protected//views/site/weather.php

You forgot to write theses functions gfImgHC() and ads721().

I guessed the first



function gfImgHC($string) {


        $url = "images/weather/" . basename($string);


        return "<img src='$url'/>";


}


but what's for the second?

thanks for your code

Thank for catching the typos. Late night effect, and copy-paste errors!

The second one, ads721() was just a function for a placeholder in the template. I took it out.

thank you