-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenWeather - Access Current Weather Data API
More file actions
108 lines (88 loc) · 2.47 KB
/
OpenWeather - Access Current Weather Data API
File metadata and controls
108 lines (88 loc) · 2.47 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
<?php
if(isset($_GET['q']) && !empty($_GET['q'])) {
$city = $_GET['q'];
}
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "api.openweathermap.org/data/2.5/weather?q=".$city."&units=metric&appid=58d0cd8ff40c7d20d2e721d3c763a0a0",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$response_str = json_decode($response);
$err = curl_error($curl);
curl_close($curl);
$currentTime = time();
if ($err) {
echo "cURL Error #:" . $err;
} else {
?>
<!doctype html>
<html>
<head>
<title>Forecast Weather using OpenWeatherMap with PHP</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">
<div class="search-city">
<form action="" method="GET">
<input type="text" name="q" placeholder="Search for city">
<input type="submit" name="submit">
</form>
</div>
<h2><?php echo $response_str->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($response_str->weather[0]->description); ?></div>
</div>
<div class="weather-forecast">
<img
src="http://openweathermap.org/img/w/<?php echo $response_str->weather[0]->icon; ?>.png"
class="weather-icon" /> <?php echo $response_str->main->temp_max; ?>°C<span
class="min-temperature"><?php echo $response_str->main->temp_min; ?>°C</span>
</div>
<div class="time">
<div>Humidity: <?php echo $response_str->main->humidity; ?> %</div>
<div>Wind: <?php echo $response_str->wind->speed; ?> km/h</div>
</div>
</div>
</body>
</html>
<?php } ?>