Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Created October 16, 2014 02:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisblakley/c5598cbb33d6fcd032f5 to your computer and use it in GitHub Desktop.
Save chrisblakley/c5598cbb33d6fcd032f5 to your computer and use it in GitHub Desktop.
Querying current weather conditions to make it rain or snow on your site.
<div id="bgimgweather">
<?php
$url = 'http://w1.weather.gov/xml/current_obs/KSYR.xml';
$xml = simplexml_load_file($url);
$currentweather = $xml->weather;
//$currentweather = "Light Rain";
?>
</div>
switch (true) {
case stristr( $currentweather, "Thunderstorm" ):
echo '<div class="bgimgrain"></div>';
echo '<div class="bgimgstorm"></div>';
break;
case stristr( $currentweather, "Snow" ):
echo '<div class="bgimgsnow"></div>';
break;
case stristr( $currentweather, "Rain" ):
case stristr( $currentweather, "Drizzle" ):
case stristr( $currentweather, "Hail" ):
case stristr( $currentweather, "Showers" ):
echo '<div class="bgimgrain"></div>';
break;
case stristr( $currentweather, "Fog" ):
case stristr( $currentweather, "Haze" ):
case stristr( $currentweather, "Smoke" ):
echo '<div class="bgimgfog"></div>';
break;
default:
//Do Nothing
break;
}
.bgimgstorm {position: absolute; top: 0; background: url('img/weather/storm.gif') repeat left top; width: 100%; height: 100%; opacity: 0.2;}
.bgimgsnow {background: url('img/weather/snow.gif') repeat left top; width: 100%; height: 100%;}
.bgimgrain {background: url('img/weather/rain.gif') repeat left top; width: 100%; height: 100%;}
.bgimgfog {background: url('img/weather/fog.png') repeat-x left top; width: 100%; height: 100%;}
<?php
$url = 'http://w1.weather.gov/xml/current_obs/KSYR.xml';
$xml = simplexml_load_file($url);
$currentweather = $xml->weather;
//$currentweather = "Light Rain";
switch (true) {
case stristr( $currentweather, "Light" ):
case stristr( $currentweather, "Drizzle" ):
echo '.bgimgfog {opacity: 0.3;} ';
echo '.bgimgrain {opacity: 0.04;} ';
echo '.bgimgsnow {opacity: 0.04;} ';
echo '/*' . $currentweather . '*/';
break;
case stristr( $currentweather, "Heavy" ):
case stristr( $currentweather, "Thunderstorm" ):
echo '.bgimgfog {opacity: 0.9;} ';
echo '.bgimgrain {opacity: 0.1;} ';
echo '.bgimgsnow {opacity: 0.2;} ';
echo '/*' . $currentweather . '*/';
break;
default:
echo '.bgimgfog {opacity: 0.5;} ';
echo '.bgimgrain {opacity: 0.07;} ';
echo '.bgimgsnow {opacity: 0.08;} ';
echo '/* ' . $currentweather . ' */';
break;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment