|
| 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 | + |
| 154 | + <input type="radio" name="units" value="I"' . $data['units_I'] . '>I - Imperial (Fahrenheit, mph, in)</input> |
| 155 | + |
| 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 | + <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 | +} |
0 commit comments