/
lab_091415.php
84 lines (74 loc) · 1.89 KB
/
lab_091415.php
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
<?php
/**
* Created by PhpStorm.
* User: steveperry
* Date: 9/14/15
* Time: 6:41 PM
*/
$cities = [
'Denver',
'New York City',
'Los Angeles'
];
$err = [];
$all_weather = get_weather($cities);
function get_weather($ary) {
$all_weather = [];
foreach ($ary as $val) {
$req = curl_init('http://api.openweathermap.org/data/2.5/weather?q='. urlencode($val));
curl_setopt($req, CURLOPT_HEADER, false);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($req);
$all_weather[$val] = json_decode($data, true);
}
return $all_weather;
}
function kelvin_to_fahrenheit($int) {
return (($int - 273.15)* 1.8) + 32;
}
function prittify_text($str) {
return ucfirst($str) . '.';
}
?>
<html>
<head>
<title>Weather</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h1>Weather in Various Cities <small>(PHP)</small></h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<table class="table table-bordered">
<thead>
<tr>
<th>City</th>
<th>Icon</th>
<th>Description</th>
<th>Temp</th>
</tr>
</thead>
<tbody>
<?php foreach ($all_weather as $key => $val) { ?>
<tr>
<td><?php echo $key; ?></td>
<td class="text-center"><?php echo '<img src="http://openweathermap.org/img/w/' . $val['weather'][0]['icon'] . '.png">'; ?></td>
<td><?php echo prittify_text($val['weather'][0]['description']); ?></td>
<td><?php echo kelvin_to_fahrenheit($val['main']['temp']) . '°'; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>