We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12168
    • 19 Posts
    This is my first snippet, iWeather. Online Demo: http://www.motoparkracing.com/iWeather.html

    The Snippet: [[iWeather]]
    <?php
    //--- iWeather by Nick Hill
    
    //---- Configuration ----------------------------------------------------------------------------------------- //
    
     // Partner ID
    $iw_partner_id = isset($partner_id) ? $partner_id : ' ';
    
    // License Key
    $iw_license_key = isset($license_key) ? $license_key : ' ';
    
    // Units Standard 's' or Metric 'm'
    $iw_units = isset($units) ? $units : 'm'; 
    
    // 1 to 5 day forecast
    $iw_days = isset($days) ? $days : '5';
     
    // City
    $iw_city = isset($city) ? $city : 'Toronto';
     
    // State/Province
    $iw_region = isset($region) ? $region : 'ON'; 
    
     // Country
    $iw_country = isset($country) ? $country : 'CA';
    
    // Image Path
    $iw_img_path = isset($icon_path) ? $icon_path : 'http://image.weather.com/web/common/wxicons'; 
    
    // Image size options 25, 31, 45, 52, 61, 72, 78, 93
    $iw_img_size = isset($icon_size) ? $icon_size : '93';                     
    
    // Image Path
    $moon_img_path = isset($moon_icon_path) ? $moon_icon_path : 'http://image.weather.com/web/common/moonicons'; 
    
    // Moon Url 25, 52
     $moon_img_size = isset($moon_icon_size) ? moon_icon_size : '25';      
    
    //----Find Location ID -------------------------------------------------------------------------------------- //	
    
    $iw_city = format_result($iw_city);
    $iw_region = format_result($iw_region);
    $iw_country = format_result($iw_country);
    $xoap_url = 'http://xoap.weather.com/search/search?where=';	
    $search_url = $xoap_url.$iw_city.'+'.$iw_region.'+'.$iw_country;
    $idsearch = simplexml_load_file($search_url);
    $loc_id = $idsearch->loc[0]->attributes()->id;
    
    //----Get XML from weather.com -------------------------------------------------------------------------------------- //
    
    $weather_url = 'http://xoap.weather.com/weather/local/';
    $iw_url = $weather_url.$loc_id.'?cc=*&dayf='.$iw_days.'&par='.$iw_partner_id.'&key='.$iw_license_key.'&unit='.$iw_units;
    $xml = simplexml_load_file($iw_url);
    
    // UNITS
            $modx->setPlaceholder('ut', $xml->head[0]->ut.'&deg'); //F or C
            $modx->setPlaceholder('ud', $xml->head[0]->ud); // Miles or KM's
            $modx->setPlaceholder('us', $xml->head[0]->us); // mph or km/h
            $modx->setPlaceholder('up', $xml->head[0]->up); // in or mb
            $modx->setPlaceholder('ur', $xml->head[0]->ur); // Inches or millimetres 
    // END
    
    // LOCATION
            $modx->setPlaceholder('location_id', $loc_id);
            $modx->setPlaceholder('city', $xml->loc[0]->dnam);
            $modx->setPlaceholder('latitude', $xml->loc[0]->lat);
            $modx->setPlaceholder('longitude', $xml->loc[0]->lon);
            $modx->setPlaceholder('sunrise', $xml->loc[0]->sunr);
            $modx->setPlaceholder('sunset', $xml->loc[0]->suns);
            $modx->setPlaceholder('time_zone', $xml->loc[0]->zone);
    // END
    
    // CURRENT CONDITIONS
            $modx->setPlaceholder('observation_time', $xml->cc[0]->lsup);
            $modx->setPlaceholder('observation_station', $xml->cc[0]->obst);
            $modx->setPlaceholder('temperature', $xml->cc[0]->tmp);
            $modx->setPlaceholder('feels_like', $xml->cc[0]->flik);
            $modx->setPlaceholder('current_condition', $xml->cc[0]->t);
            $modx->setPlaceholder('ICON', 'iw'.$xml->cc[0]->icon);
            $modx->setPlaceholder('icon', '<img class="icon_'.$iw_img_size.'" src="'.$iw_img_path.'/'.$iw_img_size.'/'.$xml->cc[0]->icon.'.png">');
            $modx->setPlaceholder('humidity', $xml->cc[0]->hmid);
            $modx->setPlaceholder('visibility', $xml->cc[0]->vis);
            $modx->setPlaceholder('dew_point', $xml->cc[0]->dewp);
    	// BAROMETRIC PRESSURE
                    $modx->setPlaceholder('barometric_pressure', $xml->cc[0]->bar[0]->r);
                    $modx->setPlaceholder('barometric_trend', $xml->cc[0]->bar[0]->d);
    	// WIND
                    $modx->setPlaceholder('wind_speed', $xml->cc[0]->wind[0]->s);
                    $modx->setPlaceholder('wind_direction', $xml->cc[0]->wind[0]->t);
    	// UV
                    $modx->setPlaceholder('uv_index', $xml->cc[0]->uv[0]->i);
                    $modx->setPlaceholder('uv_description', $xml->cc[0]->uv[0]->t);
    	// MOON
                    $modx->setPlaceholder('moon_icon', '<img class="moon_icon_'.$moon_img_size.'" src="'.$moon_img_path.'/'.$moon_img_size.'/'.$xml->cc[0]->moon[0]->icon.'.gif">');
                    $modx->setPlaceholder('moon_phase', $xml->cc[0]->moon[0]->t);
    // END 
    
    // 1 to 5 day forecast
    
    //$iTPLf = isset($forecast_tpl) ? $forecast_tpl : ' '; 
    //if ($iTPLf==' ') {} else {
            if ($iw_days <= 5) {
    	        $i = 1;
    	        while ($i <= $iw_days):
    		         $modx->setPlaceholder('forecast_day_'.$i, $xml->dayf[0]->day[$i-1]->attributes()->t);
    		         $modx->setPlaceholder('forecast_date_'.$i, $xml->dayf[0]->day[$i-1]->attributes()->dt);
    		         $modx->setPlaceholder('forecast_high_'.$i, $xml->dayf[0]->day[$i-1]->hi);
    		         $modx->setPlaceholder('forecast_low_'.$i, $xml->dayf[0]->day[$i-1]->low);
    //Day
    		         $modx->setPlaceholder('forecast_d_icon_'.$i, '<img class="icon_'.$iw_img_size.'" src="'.$iw_img_path.'/'.$iw_img_size.'/'.$xml->dayf[0]->day[$i-1]->part[0]->icon.'.png">');
    		         $modx->setPlaceholder('forecast_d_description_'.$i, $xml->dayf[0]->day[$i-1]->part[0]->t);
    		         $modx->setPlaceholder('forecast_d_windspeed_'.$i, $xml->dayf[0]->day[$i-1]->part[0]->wind[0]->s);
    		         $modx->setPlaceholder('forecast_d_winddirection_'.$i, $xml->dayf[0]->day[$i-1]->part[0]->wind[0]->t);
    		         $modx->setPlaceholder('forecast_d_percipitation_'.$i, $xml->dayf[0]->day[$i-1]->part[0]->ppcp);
    		         $modx->setPlaceholder('forecast_d_humidity'.$i, $xml->dayf[0]->day[$i-1]->part[0]->hmid);
    //Night
    		         $modx->setPlaceholder('forecast_n_icon_'.$i, '<img class="moon_icon_'.$moon_img_size.'" src="'.$moon_img_path.'/'.$moon_img_size.'/'.$xml->dayf[0]->day[$i-1]->part[1]->icon.'.gif">');
    		         $modx->setPlaceholder('forecast_n_description_'.$i, $xml->dayf[0]->day[$i-1]->part[1]->t);
    		         $modx->setPlaceholder('forecast_n_windspeed_'.$i, $xml->dayf[0]->day[$i-1]->part[1]->wind[0]->s);
    		         $modx->setPlaceholder('forecast_n_winddirection_'.$i, $xml->dayf[0]->day[$i-1]->part[1]->wind[0]->t);
    		         $modx->setPlaceholder('forecast_n_percipitation_'.$i, $xml->dayf[0]->day[$i-1]->part[1]->ppcp);
    		         $modx->setPlaceholder('forecast_n_humidity'.$i, $xml->dayf[0]->day[$i-1]->part[1]->hmid);
                             //$modx->setPlaceholder('day_'.$i, $modx->getChunk($iTPLf));
        	                 $i++;
    	        endwhile;
            }
    //}
     
    //----format the result ------------------------------------------------------------------------------------- //
    	function format_result($input)
    	{
    		return strtolower(str_replace(array(' ', '(', ')'), array('+', '+', ''), $input));
    	}
    
    //----handles the chunk ----------------------------------------------------------- //
            $iTPL = isset($tpl) ? $tpl : ' '; 
            if ($iTPL==' ') {
                   $iWoutput = '<strong>[+icon+] [+current_condition+], [+temperature+] [+ut+] in [+city+]</strong>'; 
            } else {
                   $iWoutput = $modx->getChunk($iTPL);
            }
    
    return $iWoutput;
    ?>


    The Chunk: {{iWeather}}
    <strong>[+icon+] [+current_condition+], [+temperature+] [+ut+] in [+city+] [+moon_icon+]</strong>


    Snippet Call: Get your Partner ID and License Key at https://registration.weather.com/ursa/xmloap/step1
    [!iWeather? &partner_id=`#########` &license_key=`##########` &city=`toronto` &region=`on` &country=`ca` &units=`M` &icon_size=`25` &days=`5` &tpl=`iWeather`!]






    Place Holders

    UNITS

    change units in snippet call by: &units=`m` (metric) or &units=`s` (standard)

    [+ut+] - Fahrenheit or Celsius
    [+ud+] - Miles or Km’s
    [+us+] - mph or km/h
    [+up+] - in or mb
    [+ur+] - inches or millimeters


    LOCATION

    Change location in snippet call by: &city=`` &region=`` &country=``

    [+location_id+]
    [+city+]
    [+latitude+]
    [+longitude+]
    [+sunrise+]
    [+sunset+]
    [+time_zone+]


    CURRENT CONDITIONS

    [+observation_time+]
    [+observation_station+]
    [+temperature+]
    [+feels_like+]
    [+current_condition+]
    [+humidity+]
    [+visibility+]
    [+dew_point+]
    [+barometric_pressure+]
    [+barometric_trend+]
    [+wind_speed+]
    [+wind_direction+]
    [+uv_index+]
    [+uv_description+]
    [+moon_phase+]


    IMAGES Automatically grabs images from weather.com


    [+icon+] - icons sizes: options for &icon_size=`93` (25, 31, 45, 52, 61, 72, 78 or 93)

    [+ICON+] - gives a code for the icon. example: iw27. I use this to automatically change my the look of my website using CSS based on the weather.

    [+moon_icon+] - moon icon sizes: &moon_icon_size=`52` (25 or 52)



    1 TO 5 DAY FORECAST

    # = number 1 to 5

    [+forecast_day_#+] - mon, tue, wed, thur, fri, sat, sun.
    [+forecast_date_#+]
    [+forecast_high_#+]
    [+forecast_low_#+]

    DAY
    [+forecast_d_icon_#+] - This has not been setup yet
    [+forecast_d_description_#+]
    [+forecast_d_windspeed_#+]
    [+forecast_d_winddirection_#+]
    [+forecast_d_percipitation_#+]
    [+forecast_d_humidity_#+]

    NIGHT
    [+forecast_n_icon_#+] - This has not been setup yet.
    [+forecast_n_description_#+]
    [+forecast_n_windspeed_#+]
    [+forecast_n_winddirection_#+]
    [+forecast_n_percipitation_#+]
    [+forecast_n_humidity_#+]


    PARAMATERS

    &city
    &region
    &country
    &partner_id
    &license_key
    &units
    &days
    &icon_path - "don’t end path with /"
    &icon_size
    &moon_icon_size
    &moon_icon_path
    &tpl - use to display chunk

      • 26931
      • 2,314 Posts
      hey hillnick!

      thanks for sharing! smiley