<?php

/*

Weather Thing (?)
By Eric O'Callaghan (eric@ericoc.com)
February 6, 2009 (Friday)

About:
This script just checks the weather from a Google XML API.

Requirements:
Internet connection
Web hosting with PHP and cURL module included

Notes:
 The script uses CSS to style the page; the default name
of the CSS file is weather.css, but you can remove
that (<link.. etc line) below or get a copy of my
CSS file at http://ericoc.com/weather/weather.css

*/

##########
# ENJOY! #
##########

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="weather.css">
<title>Weather</title>
</head>
<body>
<center>
<big>Weather</big>
<br><br>
<?php

// Process the form only if a zip code was provided
if (isset($_POST['zip'])) {

    
// Make sure it's a valid zip code
    
if ((is_numeric($_POST['zip'])) && (strlen($_POST['zip']) == 5)) {

        
// Create function to parse XML stuff
        
function getweather ($elementname$data) {

            
$pos1 strpos($data"<$elementname data=\"");
            
$pos2 strpos($data"\"/>"$pos1);
            
$howfar $pos2 $pos1;
            
$final substr($data$pos1$howfar);
            
$final str_replace("<$elementname data=\""""$final);
            
$final trim($final);

            return 
$final;
        }

        
// Get the zip code
        
$zip $_POST['zip'];

        
// Set the URL of where we grab the weather
        
$url "http://www.google.com/ig/api?weather=$zip";

        
// Have cURL grab the page of weather information
        
$ch curl_init($url);
        
ob_start();
        
curl_exec($ch);
        
curl_close($ch);
        
$data ob_get_contents();
        
ob_end_clean();

        
// Find the actual city name/location and make sure there is one or else give error
        
$location getweather("city"$data);

        
// Give error if invalid zip code
        
if ((!isset($location)) || (empty($location))) {
            
$error "Please enter a valid zip code.";

        
// Give error if invalid zip code
        
} elseif (eregi("(.*)(api_reply)(.*)"$location)) {
            
$error "Please enter a valid zip code.";

        
// Get all the weather information if it's a valid zip code
        
} else {

            
// Get the rest of the information
            
$conditions getweather("condition"$data);
            
$icon getweather("icon"$data);
            
$icon "http://www.google.com" $icon;
            
$temperature getweather("temp_f"$data);
            
$temperaturec getweather("temp_c"$data);
            
$humidity getweather("humidity"$data);
            
$humidity str_replace("Humidity: """$humidity);
            
$wind getweather("wind_condition"$data);

            
// Just say the wind's calm rather than "0 mph" with no direction
            
if (eregi("(.*)(at 0 mph)(.*)"$wind)) {
                
$wind "Calm";
            } else {
                
$wind str_replace("Wind: """$wind);
            }


            
$error "None";
        }
    } else {
        
$error "Please enter a valid zip code.";
    }
}

if (
$error == "None") {

    echo 
"<table>\n";
    echo 
"<tr>\n";
    echo 
"<td colspan=\"2\"><b>$location</b> <img src=\"$icon\" alt=\"$conditions\"></td>\n";
    echo 
"</tr>\n";
    echo 
"<tr>\n";
    echo 
"<td><b>Conditions</b></td>\n";
    echo 
"<td>$conditions</td>\n";
    echo 
"</tr>\n";
    echo 
"<tr>\n";
    echo 
"<td><b>Temperature</b></td>\n";
    echo 
"<td>$temperature F ($temperaturec C)</td>\n";
    echo 
"</tr>\n";
    echo 
"<tr>\n";
    echo 
"<td><b>Wind</b></td>\n";
    echo 
"<td>$wind</td>\n";
    echo 
"</tr>\n";
    echo 
"<tr>\n";
    echo 
"<td><b>Humidity</b></td>\n";
    echo 
"<td>$humidity</td>\n";
    echo 
"</tr>\n";
    echo 
"</table><br><hr><br>\n";

}

// Show any errors
if ((isset($error)) && ($error != "None")) {
    echo 
"<font color=\"red\"><b>$error</b></font><br><br>\n";
}

echo 
"Enter your zip code below to see your weather.<br>\n";
echo 
"<form method=\"post\" action=\"" $_SERVER['SCRIPT_NAME'] . "\">\n";
echo 
"<input type=\"text\" name=\"zip\" maxlength=\"5\" size=\"5\"><br><br>\n";
echo 
"<input type=\"submit\" value=\"Submit\">\n";
echo 
"</form>\n";

?>
Created by <a href="http://ericoc.com/weather/">Eric O'Callaghan</a>
</center>
</body>
</html>