For CommLab Web, we were asked to describe the current weather using the Open Weather Map, and I made this. If I were to keep going with it, I’d incorporate music and/or animations instead of (or in addition to?) the image.
<?php
$current_city = "New York";
$background_color = "#E0FFFF";
$temp_color = "red";
$temp_min = 100;
if (isset($_GET["submit"])) {
$current_city = $_GET["new_city"];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://api.openweathermap.org/data/2.5/find?q=".$current_city."&units=imperial&mode=json&cnt=0");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return_data = curl_exec($curl);
$data = json_decode($return_data);
//print($return_data);
//Once we have the raw JSON, we'll need to use PHP's JSON functions to decode it into something usable in PHP:
//$return_object = json_decode($return_data);
//print_r($data);
$temp_min = $data->list[0]->main->temp_min;
$temp_max = $data->list[0]->main->temp_max;
$temp = $data->list[0]->main->temp;
$country = $data->list[0]->sys->country;
$desc = $data->list[0]->weather[0]->description;
$icon = "http://api.openweathermap.org/img/w/".$data->list[0]->weather[0]->icon;
}
function tempColor($incoming_temp) {
if ($incoming_temp < 33) {
return "#ADD8E6";
}
elseif ($incoming_temp < 42) {
return "#B0E0E6";
}
elseif ($incoming_temp < 55) {
return "#AFEEEE";
}
elseif ($incoming_temp < 61) {
return "#F5F5DC";
}
elseif ($incoming_temp < 67) {
return "#F0E68C";
}
elseif ($incoming_temp < 79) {
return "#FFFF00";
}
elseif ($incoming_temp < 85) {
return "#FF7F50";
}
else {
return "#FF0000";
}
}
?>
/**<html>
<body style="background-color:<?php echo $background_color?>;">
**/
<div style="background-color:#fff;color:#000;padding:10px;font-size:20;">
<form method="GET" name="input" action="weather.php">
Change City: <input type="text" name="new_city" value="<?php echo $current_city ?>">
<input type="submit" name="submit" value="Submit">
</form></div>
<div style="background-color:#000;color:#fff;padding:10px">
<h2>In <?php echo(strtoupper($current_city)); echo ", "; echo "$country"; echo ", the current imperial temperature is "; echo strval($temp); echo "F with "; echo "$desc";?></h2>
</div>
<img src="<?php echo($icon); ?>" width="300px" align="right">
<div>
Today's low temperature is <?php print $temp_min ?>
<canvas id="temperature" style="background-color:<?php echo tempColor($temp_min); ?>;" height="200" width="<?php echo $temp_min *9 ?>;">
</canvas></div>
<div>
Today's high temperature is <?php print $temp_max ?>
<canvas id="temperature" style="background-color:<?php echo tempColor($temp_max); ?>;" height="200" width="<?php echo $temp_max *9 ?>;">
</canvas></div>
/**
</body>
</html>
**/