Wilt u een gastenblog schrijven voor UpService?

Wanneer u onderstaande gegevens invult nemen we zo spoedig mogelijk contact met u op.

Bedrijfsgegevens - invullen verplicht

Google Weather en CMSMS

Google heeft een onofficiele weer API die ondersteuning biedt voor een aantal plaatsen in Nederland (uiteraard ook andere landen in de wereld). In deze blog laat ik zien hoe je Google Weather in CMSMS kunt implementeren.

Voor een uitgewerkt voorbeeld zie:

Rechts onder http://www.ctrl-esc.net/

Google Weather via de browser

Wil je een overzicht van alle plaatsen in Nederland die Google weather ondersteunt ga dan naar: http://www.google.com/ig/cities?country=NL

Wil je weten wat voor weer het in Amsterdam is dan kan je dit als volgt opvragen: http://www.google.com/ig/api?weather=Amsterdam&hl=nl

Google weather implementeren binnen CMSMS

Door gebruik te maken van de plugin nh_essentials heb ik op een eenvoudige wijze Google weather kunnen koppelen aan CMSMS.

De code voor nh_api_googleweather dien je in de plugins map (web_root/plugins/function.nh_api_googleweather.php) te plaatsen:

<?php

function smarty_cms_function_nh_api_googleweather($params, &$smarty) {

    ###########################################################################
    # globals
    ###########################################################################
    
    global $gCms;
    $smarty =& $gCms->GetSmarty();
        
    ###########################################################################
    # get querystring values set defaults
    ###########################################################################
	
    if (isset($params['cache_time']) AND ($params['cache_time'] != '')) {	
        $cache_time = $params['cache_time'];
	} else {
        $cache_time = 240;
	}
	
    if (isset($params['cache_root']) AND ($params['cache_root'] != '')) {	
        $cache_root = $params['cache_root'];
	} else {
        $cache_root = "tmp/cache";
	}
    
    if (isset($params['weather']) AND ($params['weather'] != '')) {	
        $weather = $params['weather'];
	} else {
        $weather = "Amsterdam";
	}
        
    if (isset($params['hl']) AND ($params['hl'] != '')) {	
        $hl = $params['hl'];
	} else {
        $hl = "nl";
	}
    
    ###########################################################################
    # program
    ###########################################################################
    
    $data = googleweather_analyse_data($cache_time, $cache_root, $weather, $hl);
        
    $smarty->assign('forecast_conditions', $data[0]);
    $smarty->assign('current_conditions', $data[1]);
    
    return;
}

###############################################################################
# subroutines
###############################################################################

function googleweather_analyse_data($cache_time, $cache_root, $weather, $hl) {
 
    $rest       = "http://www.google.com/ig/api?weather=". urlencode($weather). "&hl=". $hl;     
	$cache      = $cache_root.'/google_weather_'. $weather. '.xml';   
    
    //print "rest: $rest <br />\n";
    //print "cache: $cache <br />\n";
    
	$result     = get_external_xml($cache, $rest, $cache_time, 1);
    $days       = array( 'zo' => 'Zondag', 'ma' => 'Maandag', 'di' => 'Dinsdag', 'wo' => 'Woensdag', 'do' => 'Donderdag', 'vr' => 'Vrijdag', 'za' => 'Zaterdag');
    
    $forecast_conditions = array();
    $current_conditions = array();
    
    if (isset($result->xml->weather->forecast_information->city['data'])) {
    
        foreach($result->xml->weather as $item) {
        
            // forecast_conditions
        
            foreach($item->forecast_conditions as $new) {
            
                $onerow = new stdClass(); 
                $day_of_week                = (string) $new->day_of_week['data']; 
                $onerow->day_of_week        = $day_of_week;
                $onerow->day_of_week_full   = $days[$day_of_week];
                $onerow->low                = $new->low['data'];
                $onerow->high               = $new->high['data'];
                $icon_parts                 = explode('/', $new->icon['data']);
                
                //print_r($icon_parts);
                
                $onerow->icon               = end($icon_parts);
                $onerow->google_icon        = 'http://www.google.com/'. $new->icon['data'];
                
                //print $onerow->google_icon. "<br >\n";
                
                $onerow->condition          = strtolower($new->condition['data']);
            
                $forecast_conditions[]      = $onerow;
            
            }
        
            // current_conditions
        
            foreach($item->current_conditions as $new) {
            
                $onerow = new stdClass(); 
                $onerow->condition      = strtolower($new->condition['data']);  
                $onerow->temp_f         = $new->temp_f['data'];  
                $onerow->temp_c         = $new->temp_c['data'];  
                $onerow->humidity       = $new->humidity['data']; 
                $icon_parts             = explode('/', $new->icon['data']);
                $onerow->icon           = end($icon_parts);
                
                //print_r($icon_parts);
                
                $onerow->google_icon    = 'http://www.google.com'. $new->icon['data'];  
                
                //print $onerow->google_icon . "<br >\n";
                
                $onerow->wind_condition = $new->wind_condition['data'];
            
                $current_conditions[]   = $onerow;
            
            }

            // echo "city: ". $item->forecast_information->city['data'] . "\n";
        } // foreach
    
    $conditions[] = $forecast_conditions;
    $conditions[] = $current_conditions;
    
    return $conditions;
    
    } else {
        return false;
    }
    
}

function smarty_cms_help_function_nh_api_googleweather() {
?>
<p style="color:#ff0000;">ATTENTION: You need the nh_api_essentials plug-in for this plug-in to work! If you experience problems, check if you have the newest version installed. Currently, at least version 0.3 is required. <br/> This message is always displayed and does not indicate any existing problem :)</p>

@todo write some documentation


<?php
}

function smarty_cms_about_function_nh_api_googleweather() {
	?>
<p>Author: Arnoud</p>
<p>Version: 0.1 (April 2010)</p>
<p>Change History:<br />
<ul>
<li>0.1 Initial publication</li>
</ul>
</p>
	<?php
}

?>

De volgende code kan je in bijvoorbeeld een global content block of de pagina waar binnen je weer wilt weergeven plaatsen:

{nh_essentials}
{nh_api_googleweather weather='Amsterdam'}

{if isset($current_conditions[0]->condition)}

<h1>Het weer in Amsterdam</h1>

<ul class="bullets_weather">
{if $current_conditions[0]->icon eq ''}
<li style="background-image: url('/images/weather/small/{$forecast_conditions[0]->icon}')">
{else}
<li style="background-image: url('/images/weather/small/{$current_conditions[0]->icon}')">
{/if}
Op dit moment {if $current_conditions[0]->condition eq ''}{$forecast_conditions[0]->condition}{else}{$current_conditions[0]->condition}{/if} {$current_conditions[0]->temp_c} &deg;C</li>
{foreach from=$forecast_conditions item=forecast name=loop}
{if ! $smarty.foreach.loop.first}
<li style="background-image: url('/images/weather/small/{$forecast->icon}')">{$forecast->day_of_week_full} {$forecast->condition} {$forecast->low} tot {$forecast->high} &deg;C</li>
{/if}
{/foreach}
</ul>

{/if}

Google weather API images

In bovenstaande template roep ik afbeeldingen (/images/weather/small/xxx) aan die overeen komen met het weer type als je zoekt op "Google weather API images" vindt je deze vanzelf.

Video

Een demostratie van bovenstaande techniek kunt u bekijken in onderstaande video.



Mobiele site UpService

Offerte aanvragen

Download ons offerte aanvraagformulier na het invullen kunt u deze op onze offerte aanvraag pagina uploaden.