'."\n"; echo ''."\n"; echo ''."\n"; echo ' Tucson Weather '."\n"; echo ''."\n"; echo '
'."\n"; # Change the target to your local weather station # for a complete list of US cities, go to # http://www.nws.noaa.gov/data/current_obs/ $target_url="http://w1.weather.gov/xml/current_obs/KTUS.xml"; $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)'; $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); curl_setopt($ch, CURLOPT_URL,$target_url); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $weatherPage = curl_exec($ch); if (!$weatherPage) { echo "
cURL error number:" .curl_errno($ch); echo "
cURL error:" . curl_error($ch); exit; } # don't let this script run for more than 60 seconds # set_time_limit(60); # create file handles if needed if (!defined('STDIN')) { define('STDIN', fopen('php://stdin', 'r')); } if (!defined('STDOUT')) { define('STDOUT', fopen('php://stdout', 'w')); } if (!defined('STDERR')) { define('STDERR', fopen('php://stderr', 'w')); } # retrieve this web page # get_contents no longer allowed, uses curl above. # $weatherPage=file_get_contents($weatherURL); # get temperature in Fahrenheit if (preg_match("/([0-9.]+)<\/temp_f>/i",$weatherPage,$matches)) {$currentTemp=$matches[1];} # echo ($currentTemp); print 'Temperature ' ; print ($currentTemp); print ' degrees F'; echo "\n"; # get wind direction if (preg_match("/North<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='North'; } elseif (preg_match("/South<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='South'; } elseif (preg_match("/East<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='East'; } elseif (preg_match("/West<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='West'; } elseif (preg_match("/Northwest<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='Northwest'; } elseif (preg_match("/Northeast<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='Northeast'; } elseif (preg_match("/Southwest<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='Southwest'; } elseif (preg_match("/Southeast<\/wind_dir>/i",$weatherPage)) { $currentWindDirection='Southeast'; } # echo ($currentWindDirection); # get wind speed if (preg_match("/([0-9]+)([\.]+)([0-9]+)<\/wind_mph>/i",$weatherPage,$matches)) { $currentWindSpeed = $matches[1]; } # echo ($currentWindSpeed); if ($currentWindDirection && $currentWindSpeed) { print '
Wind from the '; print ($currentWindDirection); # echo "\n"; print ' at ' ; print ($currentWindSpeed); print ' MPH
'; echo "\n"; } # get relative humidity if (preg_match("/([0-9.]+)<\/relative_humidity>/i",$weatherPage,$matches)) {$currentHumidity=$matches[1];} # echo ($currentHumidity); print '
Humidity ' ; print ($currentHumidity); print "%"; echo "\n"; # get visibility in miles if (preg_match("/([0-9.]+)<\/visibility_mi>/i",$weatherPage,$matches)) {$currentVisibility=$matches[1];} # echo ($currentVisibility); print 'Visibility ' ; print ($currentVisibility); print ' miles
'; echo "\n"; # get barometric pressure if (preg_match("/([0-9]+)([\.]+)([0-9]+)<\/pressure_in>/i",$weatherPage,$matches)) { $currentPressure1 = $matches[1]; $currentPressure2 = $matches[3]; } # echo ($currentPressure1.$currentPressure2); print '
Barometric pressure ' ; print ($currentPressure1); print "."; print ($currentPressure2); print ' in
'; echo "\n"; echo '
'."\n"; echo' '."\n"; echo ''."\n"; ?>