Google Weather API Datasource

by anachronist
This Datasource delivers the weather forecast based on zip and country using the google weather api
conditions in the model for the datasource:
['condition']['zip'] = Zip Code
['condition']['country'] = Country


PHP Snippet:

<?php 
<?php

class WeatherSource extends DataSource {

public function 
read($model$queryData = array()) {
    
    
$dom = new DOMDocument;
    
$dom->loadXML(utf8_encode(
                
file_get_contents("http://www.google.com/ig/api?weather=".$queryData['conditions']['zip']."-".$queryData['conditions']['country']."&hl=en")
                ));
    if (!
$dom) {
        echo 
'Error while parsing the document';
        exit;
    }
    
    
$s simplexml_import_dom($dom);
    
    
//aktuelles wetter
    
$return[0]['condition'] = strtolower((string)$s->weather->current_conditions->condition['data']);
    
$return[0]['low'] = (string)$s->weather->current_conditions->temp_f['data'];
    
$return[0]['high'] = (string)$s->weather->current_conditions->temp_f['data'];
    
$return[0]['humidity'] = str_replace("Humidity: """, (string)$s->weather->current_conditions->humidity['data']);
    
$return[0]['wind_condition'] = (string)$s->weather->current_conditions->wind_condition['data'];
    
ereg("Wind: ([W|S|E|N]) at ([0-9]+) mph"$return[0]['wind_condition'], $wind);
    
$return[0]['speed'] = $wind[2];
    
$return[0]['direction'] = $wind[1];
    
$return[0]['date'] = strtotime($s->weather->forecast_information->forecast_date['data']);
    
    
//wetter fuer die naechsten tage.
    
    
$i 1;
    foreach(
$s->weather->forecast_conditions As $forecast){
        
        
$return[$i]['condition'] = strtolower((string)$forecast->condition['data']);
        
$return[$i]['low'] = (string)$forecast->low['data'];
        
$return[$i]['high'] = (string)$forecast->high['data'];
        
$return[$i]['date'] = strtotime($s->weather->forecast_information->forecast_date['data']) + ($i 86400);
            
        
$i++;
    }
    
    
    
    return 
$return;
    
        
    }
}

?>
?>

Report

More on Models

Advertising

Comments

  • kages posted on 12/08/10 10:05:45 AM
    Sorry for all the posts, I have no idea how formatting is respected by this forum. Instructions would be useful on the comment form *hint hint*

    Anyways, most servers will reject file_get_contents, so you'll have to use Curl Instead.

    $url = "http://www.google.com/ig/api?weather=".$queryData['conditions']['zip']."-".$queryData['conditions']['country']."&hl=en";
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $url);
    $contents = curl_exec($c);
    curl_close($c);

    $dom = new DOMDocument;
    $dom->loadXML(utf8_encode($contents
    ));
  • kages posted on 12/08/10 10:03:23 AM
    loadXML(utf8_encode($contents
    ));
    if (!$dom) {
    echo 'Error while parsing the document';
    exit;
    }

    $s = simplexml_import_dom($dom);

    //aktuelles wetter
    $return[0]['condition'] = strtolower((string)$s->weather->current_conditions->condition['data']);
    $return[0]['low'] = (string)$s->weather->current_conditions->temp_f['data'];
    $return[0]['high'] = (string)$s->weather->current_conditions->temp_f['data'];
    $return[0]['humidity'] = str_replace("Humidity: ", "", (string)$s->weather->current_conditions->humidity['data']);
    $return[0]['wind_condition'] = (string)$s->weather->current_conditions->wind_condition['data'];
    ereg("Wind: ([W|S|E|N]) at ([0-9]+) mph", $return[0]['wind_condition'], $wind);
    $return[0]['speed'] = $wind[2];
    $return[0]['direction'] = $wind[1];
    $return[0]['date'] = strtotime($s->weather->forecast_information->forecast_date['data']);

    //wetter fuer die naechsten tage.

    $i = 1;
    foreach($s->weather->forecast_conditions As $forecast){

    $return[$i]['condition'] = strtolower((string)$forecast->condition['data']);
    $return[$i]['low'] = (string)$forecast->low['data'];
    $return[$i]['high'] = (string)$forecast->high['data'];
    $return[$i]['date'] = strtotime($s->weather->forecast_information->forecast_date['data']) + ($i * 86400);

    $i++;
    }



    return $return;


    }
    }

    ?>
  • kages posted on 12/08/10 10:02:18 AM

    loadXML(utf8_encode($contents
                    ));
        if (!$dom) {
            echo 'Error while parsing the document';
            exit;
        }
        
        $s = simplexml_import_dom($dom);
        
        //aktuelles wetter
        $return[0]['condition'] = strtolower((string)$s->weather->current_conditions->condition['data']);
        $return[0]['low'] = (string)$s->weather->current_conditions->temp_f['data'];
        $return[0]['high'] = (string)$s->weather->current_conditions->temp_f['data'];
        $return[0]['humidity'] = str_replace("Humidity: ", "", (string)$s->weather->current_conditions->humidity['data']);
        $return[0]['wind_condition'] = (string)$s->weather->current_conditions->wind_condition['data'];
        ereg("Wind: ([W|S|E|N]) at ([0-9]+) mph", $return[0]['wind_condition'], $wind);
        $return[0]['speed'] = $wind[2];
        $return[0]['direction'] = $wind[1];
        $return[0]['date'] = strtotime($s->weather->forecast_information->forecast_date['data']);
        
        //wetter fuer die naechsten tage.
        
        $i = 1;
        foreach($s->weather->forecast_conditions As $forecast){
            
            $return[$i]['condition'] = strtolower((string)$forecast->condition['data']);
            $return[$i]['low'] = (string)$forecast->low['data'];
            $return[$i]['high'] = (string)$forecast->high['data'];
            $return[$i]['date'] = strtotime($s->weather->forecast_information->forecast_date['data']) + ($i * 86400);
                
            $i++;
        }
        
        
        
        return $return;
        
            
        }
    }

    ?>
  • kages posted on 12/08/10 10:00:15 AM
    Most servers will reject file_get_contents as of PHP5. Below is the code with this fixed:


    loadXML(utf8_encode($contents
    ));
    if (!$dom) {
    echo 'Error while parsing the document';
    exit;
    }

    $s = simplexml_import_dom($dom);

    //aktuelles wetter
    $return[0]['condition'] = strtolower((string)$s->weather->current_conditions->condition['data']);
    $return[0]['low'] = (string)$s->weather->current_conditions->temp_f['data'];
    $return[0]['high'] = (string)$s->weather->current_conditions->temp_f['data'];
    $return[0]['humidity'] = str_replace("Humidity: ", "", (string)$s->weather->current_conditions->humidity['data']);
    $return[0]['wind_condition'] = (string)$s->weather->current_conditions->wind_condition['data'];
    ereg("Wind: ([W|S|E|N]) at ([0-9]+) mph", $return[0]['wind_condition'], $wind);
    $return[0]['speed'] = $wind[2];
    $return[0]['direction'] = $wind[1];
    $return[0]['date'] = strtotime($s->weather->forecast_information->forecast_date['data']);

    //wetter fuer die naechsten tage.

    $i = 1;
    foreach($s->weather->forecast_conditions As $forecast){

    $return[$i]['condition'] = strtolower((string)$forecast->condition['data']);
    $return[$i]['low'] = (string)$forecast->low['data'];
    $return[$i]['high'] = (string)$forecast->high['data'];
    $return[$i]['date'] = strtotime($s->weather->forecast_information->forecast_date['data']) + ($i * 86400);

    $i++;
    }



    return $return;


    }
    }

    ?>
  • rimedio posted on 12/07/10 06:56:59 AM
    Im pretty new to cakephp and I was wondering if you could explain a little further the usage of the datasource.
    I understand it needs to be placed on
    /models/datasources/GoogleWeather.php

    Then you need to create a file on /models/
    Setup the /config/database.php

    And at last invoke it thru in the controller and use it in the view.
    am I right? Or totally wrong :) ?

    Thanks for your time in advance.
    Rafa
login to post a comment.