Skip to content

Commit b0cb3ab

Browse files
authored
Merge pull request #8 from attogram/v2
V2 - setLocations
2 parents b5f606d + e09a998 commit b0cb3ab

File tree

5 files changed

+590
-193
lines changed

5 files changed

+590
-193
lines changed

README.md

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,56 @@ Versions:
2020

2121
```php
2222
<?php
23-
2423
require('path/to/vendor/autoload.php');
2524

25+
$weatherbit = new \Attogram\Weatherbit\Weatherbit();
26+
2627
try {
27-
$weatherbit = new \Attogram\Weatherbit\Weatherbit();
2828
$weatherbit->setKey('YOUR-WEATHERBIT-API-KEY');
29-
$weatherbit->setCity('Amsterdam');
30-
$weatherbit->setCountry('NL');
31-
29+
$weatherbit->setLocationByCity('Amsterdam', 'NL');
3230
$currentWeather = $weatherbit->getCurrent(); // Gets array of current weather data
33-
34-
$forecastedWeather = $weatherbit->getDailyForecast(15); // Gets array 15 day forecast
35-
3631
} catch (Exception $exception) {
3732
exit('Error: ' . $exception->getMessage());
3833
}
3934

40-
print "Current Weather:\n";
4135
print_r($currentWeather);
36+
```
4237

43-
print "Forecasted Weather:\n";
44-
print_r($forecastedWeather);
38+
* see [public/test.php](public/example.php) for an example web form
4539

46-
```
40+
## Functions
41+
42+
### public function setKey(string $key)
43+
44+
### public function setLanguage(string $languageCode)
45+
46+
### public function setUnits(string $unitsCode)
47+
48+
### public function setLocationByLatitudeLongitude(string $latitude, string $longitude)
49+
50+
### public function setLocationByCityId(string $cityId)
51+
52+
### public function setLocationByPostalCode(string $postalCode)
53+
54+
### public function setLocationByCityIds(array $cityIds)
55+
56+
### public function setLocationByCity(string $city, string $country = '')
57+
58+
### public function setLocationByIp(string $ipAddress = 'auto')
59+
60+
### public function setLocationByStation(string $weatherStation)
61+
62+
### public function setLocationByStations(array $weatherStations)
63+
64+
### public function getDailyForecast($days = 10): array
65+
66+
### public function getCurrent(): array
67+
68+
### public function getUsage(): array
4769

48-
* see [public/test.php](public/test.php) for an example web form
70+
### public function getUrl(): string
4971

50-
## Links
72+
## Project Links
5173

5274
* Github: <https://github.com/attogram/weatherbit-api-wrapper/>
5375
* Packagist: <https://packagist.org/packages/attogram/weatherbit-api-wrapper>

public/example.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Example of using the Weatherbit API Wrapper
4+
*
5+
* @see https://github.com/attogram/weatherbit-api-wrapper
6+
*/
7+
declare(strict_types = 1);
8+
9+
use \Attogram\Weatherbit\Weatherbit;
10+
11+
ini_set('display_errors', '1');
12+
ini_set('display_startup_errors', '1');
13+
error_reporting(E_ALL);
14+
15+
require('../src/Weatherbit.php'); // or: require('path/to/vendor/autoload.php');
16+
17+
$data = [];
18+
19+
$data['call'] = isset($_GET['call']) ? $_GET['call'] : 'usage';
20+
$data['key'] = isset($_GET['key']) ? $_GET['key'] : '';
21+
$data['units'] = isset($_GET['units']) ? $_GET['units'] : 'M';
22+
$data['language'] = isset($_GET['language']) ? $_GET['language'] : 'en';
23+
$data['city'] = isset($_GET['city']) ? $_GET['city'] : '';
24+
$data['country'] = isset($_GET['country']) ? $_GET['country'] : '';
25+
$data['days'] = isset($_GET['days']) ? intval($_GET['days']) : 2;
26+
$data['ipAddress'] = isset($_GET['ip']) ? $_GET['ip'] : '';
27+
$data['latitude'] = isset($_GET['lat']) ? $_GET['lat'] : '';
28+
$data['longitude'] = isset($_GET['long']) ? $_GET['long'] : '';
29+
30+
$check = ' checked';
31+
$data['call_forecast'] = ($data['call'] == 'forecast') ? $check : '';
32+
$data['call_current'] = ($data['call'] == 'current') ? $check : '';
33+
$data['call_usage'] = ($data['call'] == 'usage') ? $check : '';
34+
$data['units_M'] = ($data['units'] == 'M') ? $check : '';
35+
$data['units_I'] = ($data['units'] == 'I') ? $check : '';
36+
$data['units_S'] = ($data['units'] == 'S') ? $check : '';
37+
38+
$data['pageTitle'] = 'weatherbit-api-wrapper v' . Weatherbit::VERSION;
39+
40+
printForm();
41+
42+
if (!isset($_GET['run']) || $_GET['run'] != 'test') {
43+
printFooter();
44+
exit;
45+
}
46+
47+
$response = [];
48+
$error = '';
49+
50+
$weatherbit = new Weatherbit();
51+
52+
try {
53+
54+
$weatherbit->setKey($data['key']);
55+
56+
if (!empty($data['units'] && $data['units'] != 'M')) {
57+
$weatherbit->setUnits($data['units']);
58+
}
59+
60+
if (!empty($data['language'] && $data['language'] != 'en')) {
61+
$weatherbit->setLanguage($data['language']);
62+
}
63+
64+
if (!empty($data['city'])) {
65+
$weatherbit->setLocationByCity($data['city'], $data['country']);
66+
} elseif (!empty($data['latitude'])) {
67+
$weatherbit->setLocationByLatitudeLongitude($data['latitude'], $data['longitude']);
68+
} elseif (!empty($data['ipAddress'])) {
69+
$weatherbit->setLocationByIP($data['ipAddress']);
70+
}
71+
72+
switch ($data['call']) {
73+
case 'forecast':
74+
$response = $weatherbit->getDailyForecast($data['days']);
75+
break;
76+
case 'current':
77+
$response = $weatherbit->getCurrent();
78+
break;
79+
case 'usage':
80+
$response = $weatherbit->getUsage();
81+
break;
82+
default:
83+
$error = 'Invalid Call type';
84+
break;
85+
}
86+
} catch (Exception $error) {
87+
$error = $error->getMessage();
88+
}
89+
90+
printResults($response, $error);
91+
92+
printFooter();
93+
94+
function printResults($response, $error)
95+
{
96+
global $weatherbit;
97+
98+
print '<pre>API Call URL: <a href="' . $weatherbit->getUrl() . '" target="_blank">'
99+
. $weatherbit->getUrl() . '</a></pre>';
100+
101+
if ($error) {
102+
print '<p style="background-color:lightsalmon;padding:10px;">ERROR: ' . $error . '</p>';
103+
}
104+
105+
if ($response) {
106+
print '<pre style="background-color:lightgreen;padding:10px;">RESPONSE: ' . print_r($response, true) . '</pre>';
107+
}
108+
}
109+
110+
function printForm()
111+
{
112+
global $data;
113+
114+
print '<html><head><title>' . $data['pageTitle'] . '</title></head><body>
115+
<h1>' . $data['pageTitle'] . '</h1>
116+
<form>
117+
<dl>
118+
<dt>API Call:<dt>
119+
<dd><input type="radio" name="call" value="forecast"' . $data['call_forecast']
120+
. '>Daily Weather Forecast (1-16 days)</input> - ' . Weatherbit::POSTFIX_FORECAST_DAILY . '</dd>
121+
<dd><input type="radio" name="call" value="current"' . $data['call_current']
122+
. '>Current Weather</input> - ' . Weatherbit::POSTFIX_CURRENT . '</dd>
123+
<dd><input type="radio" name="call" value="usage"' . $data['call_usage']
124+
. '>API Usage</input> - ' . Weatherbit::POSTFIX_USAGE . '</dd>
125+
</dl>
126+
127+
API Key: <input name="key" type="text" value="' . htmlentities($data['key']) . '" size="35" /><br />
128+
129+
<dl>
130+
<dt>Location by City:</dt>
131+
<dd> City: <input name="city" type="text" value="' . htmlentities($data['city']) . '" size="20" /></dd>
132+
<dd>Country: <input name="country" type="text" value="' . htmlentities($data['country']) . '" size="2" maxlength="2" />
133+
(2 Letter Country Code) (optional)</dd>
134+
<br />
135+
<dt>Location By Latitude / Longitude:</dt>
136+
<dd>Latitude: <input name="lat" type="text" value="' . $data['latitude'] . '" size="20" /></dd>
137+
<dd>Longitude: <input name="long" type="text" value="' . $data['longitude'] . '" size="20" /></dd>
138+
<br />
139+
<dt>Location By IP Address:</dt>
140+
<dd>IP Address: <input name="ip" type="text" value="' . $data['ipAddress'] . '" size="20" /></dd>
141+
</dl>
142+
143+
Forecast Days (1-16): <input name="days" type="text" value="' . $data['days'] . '" size="2" maxlength="2" />
144+
<br />
145+
146+
Language: <input name="language" type="text" value="' . htmlentities($data['language']) . '" size="2" maxlength="2" />
147+
(2 Letter Language code)
148+
149+
<br />
150+
151+
Units:
152+
<input type="radio" name="units" value="M"' . $data['units_M'] . '>M - Metric (Celcius, m/s, mm)</input>
153+
&nbsp;
154+
<input type="radio" name="units" value="I"' . $data['units_I'] . '>I - Imperial (Fahrenheit, mph, in)</input>
155+
&nbsp;
156+
<input type="radio" name="units" value="S"' . $data['units_S'] . '>S - Scientific (Kelvin, m/s, mm)</input>
157+
</select>
158+
<br />
159+
<br />
160+
<input type="hidden" name="run" value="test" />
161+
<input type="submit" value=" Get Weatherbit.io API Response " />
162+
&nbsp; &nbsp; <a href="' . $_SERVER['PHP_SELF'] . '">reset</a>
163+
</form>';
164+
}
165+
166+
function printFooter()
167+
{
168+
print '<br /><hr />'
169+
. '<a href="https://github.com/attogram/weatherbit-api-wrapper/" target="_blank">'
170+
. 'https://github.com/attogram/weatherbit-api-wrapper/</a>'
171+
. '<br /><br />' . gmdate('Y-m-d H:i:s') . ' UTC'
172+
. '</body></html>';
173+
}

public/test.php

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)