-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
111 lines (94 loc) · 2.77 KB
/
index.php
File metadata and controls
111 lines (94 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
require_once('request.php');
class weatherAPI{
function getWeatherReport($city){
$api_key = "API KEY HERE";
$url = "https://api.openweathermap.org/data/2.5/weather?q=$city&appid=$api_key";
$response = SimpleJsonRequest::get($url);
return json_encode($response, JSON_PRETTY_PRINT);
}
}
$weatherAPI = new weatherAPI();
if (isset($_GET['city'])){
try {
$response = $weatherAPI->getWeatherReport($_GET['city']);
$data = json_decode($response);
$currentTime = time();
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
?>
<!doctype html>
<html>
<head>
<title>Weather API</title>
<style>
body {
font-family: Arial;
font-size: 0.95em;
color: #929292;
}
.report-container {
border: #E0E0E0 1px solid;
padding: 20px 40px 40px 40px;
border-radius: 2px;
width: 550px;
margin: 0 auto;
}
.weather-icon {
vertical-align: middle;
margin-right: 20px;
}
.weather-forecast {
color: #212121;
font-size: 1.2em;
font-weight: bold;
margin: 20px 0px;
}
span.min-temperature {
margin-left: 15px;
color: #929292;
}
.time {
line-height: 25px;
}
</style>
</head>
<body>
<div class="report-container">
<h2><?php echo $data->name; ?> Weather Status</h2>
<div class="time">
<div><?php echo date("l g:i a", $currentTime); ?></div>
<div><?php echo date("jS F, Y",$currentTime); ?></div>
<div><?php echo ucwords($data->weather[0]->description); ?></div>
</div>
<div class="weather-forecast">
<img
src="http://openweathermap.org/img/w/<?php echo $data->weather[0]->icon; ?>.png"
class="weather-icon" /> <?php echo $data->main->temp_max; ?>°C<span
class="min-temperature"><?php echo $data->main->temp_min; ?>°C</span>
</div>
<div class="time">
<div>Humidity: <?php echo $data->main->humidity; ?> %</div>
<div>Wind: <?php echo $data->wind->speed; ?> km/h</div>
</div>
</div>
</body>
</html>
<?php
}
elseif(isset($_POST['city'])) {
try {
$response = $weatherAPI->getWeatherReport($_POST['city']);
echo $response;
} catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
}
}
else {
echo '<div class="report-container">';
echo '<h2>Please pass City or Country in URL</h2>';
echo '</div>';
}
?>