-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.php
172 lines (142 loc) · 6.34 KB
/
example.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Example of using the Weatherbit API Wrapper
*
* @see https://github.com/attogram/weatherbit-api-wrapper
*/
declare(strict_types=1);
use Attogram\Weatherbit\Weatherbit;
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
require('../vendor/autoload.php');
$data = [];
$data['call'] = isset($_GET['call']) ? $_GET['call'] : 'usage';
$data['key'] = isset($_GET['key']) ? $_GET['key'] : '';
$data['units'] = isset($_GET['units']) ? $_GET['units'] : 'M';
$data['language'] = isset($_GET['language']) ? $_GET['language'] : 'en';
$data['city'] = isset($_GET['city']) ? $_GET['city'] : '';
$data['country'] = isset($_GET['country']) ? $_GET['country'] : '';
$data['days'] = isset($_GET['days']) ? intval($_GET['days']) : 2;
$data['ipAddress'] = isset($_GET['ip']) ? $_GET['ip'] : '';
$data['latitude'] = isset($_GET['lat']) ? $_GET['lat'] : '';
$data['longitude'] = isset($_GET['long']) ? $_GET['long'] : '';
$check = ' checked';
$data['call_forecast'] = ($data['call'] == 'forecast') ? $check : '';
$data['call_current'] = ($data['call'] == 'current') ? $check : '';
$data['call_usage'] = ($data['call'] == 'usage') ? $check : '';
$data['units_M'] = ($data['units'] == 'M') ? $check : '';
$data['units_I'] = ($data['units'] == 'I') ? $check : '';
$data['units_S'] = ($data['units'] == 'S') ? $check : '';
$data['pageTitle'] = 'weatherbit-api-wrapper v' . Weatherbit::VERSION;
printForm();
if (!isset($_GET['run']) || $_GET['run'] != 'test') {
printFooter();
exit;
}
$response = [];
$error = '';
$weatherbit = new Weatherbit();
try {
$weatherbit->setKey($data['key']);
if (!empty($data['units'] && $data['units'] != 'M')) {
$weatherbit->setUnits($data['units']);
}
if (!empty($data['language'] && $data['language'] != 'en')) {
$weatherbit->setLanguage($data['language']);
}
if (!empty($data['city'])) {
$weatherbit->setLocationByCity($data['city'], $data['country']);
} elseif (!empty($data['latitude'])) {
$weatherbit->setLocationByLatitudeLongitude($data['latitude'], $data['longitude']);
} elseif (!empty($data['ipAddress'])) {
$weatherbit->setLocationByIP($data['ipAddress']);
}
switch ($data['call']) {
case 'forecast':
$response = $weatherbit->getDailyForecast($data['days']);
break;
case 'current':
$response = $weatherbit->getCurrent();
break;
case 'usage':
$response = $weatherbit->getUsage();
break;
default:
$error = 'Invalid Call type';
break;
}
} catch (Exception $error) {
$error = get_class($error) . ': ' . $error->getMessage();
}
printResults($response, $error);
printFooter();
function printResults($response, $error)
{
global $weatherbit;
print '<pre>API Call URL: <a href="' . $weatherbit->getUrl() . '" target="_blank">'
. $weatherbit->getUrl() . '</a></pre>';
if ($error) {
print '<p style="background-color:lightsalmon;padding:10px;">ERROR: ' . $error . '</p>';
}
if ($response) {
print '<pre style="background-color:lightgreen;padding:10px;">RESPONSE: ' . print_r($response, true) . '</pre>';
}
}
function printForm()
{
global $data;
print '<html><head><title>' . $data['pageTitle'] . '</title></head><body>
<h1>' . $data['pageTitle'] . '</h1>
<form>
<dl>
<dt>API Call:<dt>
<dd><input type="radio" name="call" value="forecast"' . $data['call_forecast']
. '>Daily Weather Forecast (1-16 days)</input> - ' . Weatherbit::POSTFIX_FORECAST_DAILY . '</dd>
<dd><input type="radio" name="call" value="current"' . $data['call_current']
. '>Current Weather</input> - ' . Weatherbit::POSTFIX_CURRENT . '</dd>
<dd><input type="radio" name="call" value="usage"' . $data['call_usage']
. '>API Usage</input> - ' . Weatherbit::POSTFIX_USAGE . '</dd>
</dl>
API Key: <input name="key" type="text" value="' . htmlentities($data['key']) . '" size="35" /><br />
<dl>
<dt>Location by City:</dt>
<dd> City: <input name="city" type="text" value="' . htmlentities($data['city'])
. '" size="20" /></dd>
<dd>Country: <input name="country" type="text" value="' . htmlentities($data['country'])
. '" size="2" maxlength="2" /> (2 Letter Country Code) (optional)</dd>
<br />
<dt>Location By Latitude / Longitude:</dt>
<dd>Latitude: <input name="lat" type="text" value="' . $data['latitude'] . '" size="20" /></dd>
<dd>Longitude: <input name="long" type="text" value="' . $data['longitude'] . '" size="20" /></dd>
<br />
<dt>Location By IP Address:</dt>
<dd>IP Address: <input name="ip" type="text" value="' . $data['ipAddress'] . '" size="20" /></dd>
</dl>
Forecast Days (1-16): <input name="days" type="text" value="' . $data['days'] . '" size="2" maxlength="2" />
<br />
Language: <input name="language" type="text" value="' . htmlentities($data['language'])
. '" size="2" maxlength="2" /> (2 Letter Language code)
<br />
Units:
<input type="radio" name="units" value="M"' . $data['units_M'] . '>M - Metric (Celcius, m/s, mm)</input>
<input type="radio" name="units" value="I"' . $data['units_I'] . '>I - Imperial (Fahrenheit, mph, in)</input>
<input type="radio" name="units" value="S"' . $data['units_S'] . '>S - Scientific (Kelvin, m/s, mm)</input>
</select>
<br />
<br />
<input type="hidden" name="run" value="test" />
<input type="submit" value=" Get Weatherbit.io API Response " />
<a href="' . $_SERVER['PHP_SELF'] . '">reset</a>
</form>';
}
function printFooter()
{
print '<br /><hr />'
. '<a href="https://github.com/attogram/weatherbit-api-wrapper/" target="_blank">'
. 'https://github.com/attogram/weatherbit-api-wrapper/</a>'
. '<br /><br />' . gmdate('Y-m-d H:i:s') . ' UTC'
. '</body></html>';
}