├── .gitignore ├── README.md ├── example.php └── lib └── forecast.io.php /.gitignore: -------------------------------------------------------------------------------- 1 | /nbproject/private 2 | /nbproject/ 3 | .DS_Store 4 | .idea 5 | example-test.php 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | forecast.io-php-api 2 | =================== 3 | 4 | forecast.io PHP API 5 | 6 | To find out how it works check out the example.php 7 | 8 | # Releases 9 | 10 | * 2015-03-10: Did some refactoring and added units and language as class properties. You can set them via constructor 11 | 12 | # Contact 13 | 14 | Web: [www.tricd.de](http://www.tricd.de) 15 | 16 | Email: tobias.redmann@tricd.de -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | '; 5 | 6 | $latitude = '52.4308'; 7 | $longitude = '13.2588'; 8 | $units = 'auto'; // Can be set to 'us', 'si', 'ca', 'uk' or 'auto' (see forecast.io API); default is auto 9 | $lang = 'en'; // Can be set to 'en', 'de', 'pl', 'es', 'fr', 'it', 'tet' or 'x-pig-latin' (see forecast.io API); default is 'en' 10 | 11 | $forecast = new ForecastIO($api_key, $units, $lang); 12 | 13 | // all default will be 14 | // $forecast = new ForecastIO($api_key); 15 | 16 | 17 | /* 18 | * GET CURRENT CONDITIONS 19 | */ 20 | $condition = $forecast->getCurrentConditions($latitude, $longitude); 21 | 22 | echo 'Current temperature: '.$condition->getTemperature(). "\n"; 23 | 24 | 25 | /* 26 | * GET HOURLY CONDITIONS FOR TODAY 27 | */ 28 | $conditions_today = $forecast->getForecastToday($latitude, $longitude); 29 | 30 | echo "\n\nTodays temperature:\n"; 31 | 32 | foreach($conditions_today as $cond) { 33 | 34 | echo $cond->getTime('H:i:s') . ': ' . $cond->getTemperature(). "\n"; 35 | 36 | } 37 | 38 | /* 39 | * GET DAILY CONDITIONS FOR NEXT 7 DAYS 40 | */ 41 | $conditions_week = $forecast->getForecastWeek($latitude, $longitude); 42 | 43 | echo "\n\nConditions this week:\n"; 44 | 45 | foreach($conditions_week as $conditions) { 46 | 47 | echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMaxTemperature() . "\n"; 48 | 49 | } 50 | 51 | /* 52 | * GET HISTORICAL CONDITIONS 53 | */ 54 | $condition = $forecast->getHistoricalConditions($latitude, $longitude, '2010-10-10T14:00:00-0700'); 55 | 56 | echo "\n\nTemperatur 2010-10-10: ". $condition->getMaxTemperature(). "\n"; 57 | 58 | 59 | -------------------------------------------------------------------------------- /lib/forecast.io.php: -------------------------------------------------------------------------------- 1 | api_key = $api_key; 25 | $this->units = $units; 26 | $this->language = $language; 27 | 28 | } 29 | 30 | /** 31 | * @return string 32 | */ 33 | public function getUnits() 34 | { 35 | return $this->units; 36 | } 37 | 38 | /** 39 | * @param string $units 40 | */ 41 | public function setUnits($units) 42 | { 43 | $this->units = $units; 44 | } 45 | 46 | /** 47 | * @return string 48 | */ 49 | public function getLanguage() 50 | { 51 | return $this->language; 52 | } 53 | 54 | /** 55 | * @param string $language 56 | */ 57 | public function setLanguage($language) 58 | { 59 | $this->language = $language; 60 | } 61 | 62 | 63 | private function requestData($latitude, $longitude, $timestamp = false, $exclusions = false) 64 | { 65 | 66 | $validUnits = array('auto', 'us', 'si', 'ca', 'uk'); 67 | 68 | if (in_array($this->units, $validUnits)) { 69 | 70 | $request_url = self::API_ENDPOINT . 71 | $this->api_key . '/' . 72 | $latitude . ',' . $longitude . 73 | ($timestamp ? ',' . $timestamp : '') . 74 | '?units=' . $this->units . '&lang=' . $this->language . 75 | 76 | ($exclusions ? '&exclude=' . $exclusions : ''); 77 | 78 | /** 79 | * Use Buffer to cache API-requests if initialized 80 | * (if not, just get the latest data) 81 | * 82 | * More info: http://git.io/FoO2Qw 83 | */ 84 | 85 | if (class_exists('Buffer')) { 86 | $cache = new Buffer(); 87 | $content = $cache->data($request_url); 88 | } else { 89 | $content = file_get_contents($request_url); 90 | } 91 | 92 | } else { 93 | 94 | return false; 95 | 96 | } 97 | 98 | if (!empty($content)) { 99 | 100 | return json_decode($content); 101 | 102 | } else { 103 | 104 | return false; 105 | 106 | } 107 | 108 | 109 | } 110 | 111 | /** 112 | * Will return the current conditions 113 | * 114 | * @param float $latitude 115 | * @param float $longitude 116 | * @return \ForecastIOConditions|boolean 117 | */ 118 | public function getCurrentConditions($latitude, $longitude) 119 | { 120 | 121 | $data = $this->requestData($latitude, $longitude); 122 | 123 | if ($data !== false) { 124 | 125 | return new ForecastIOConditions($data->currently); 126 | 127 | } else { 128 | 129 | return false; 130 | 131 | } 132 | 133 | } 134 | 135 | /** 136 | * Will return historical conditions for day of given timestamp 137 | * 138 | * @param float $latitude 139 | * @param float $longitude 140 | * @param int $timestamp 141 | * @return \ForecastIOConditions|boolean 142 | */ 143 | public function getHistoricalConditions($latitude, $longitude, $timestamp) 144 | { 145 | 146 | $exclusions = 'currently,minutely,hourly,alerts,flags'; 147 | 148 | $data = $this->requestData($latitude, $longitude, $timestamp, $exclusions); 149 | 150 | if ($data !== false) { 151 | 152 | return new ForecastIOConditions($data->daily->data[0]); 153 | 154 | } else { 155 | 156 | return null; 157 | 158 | } 159 | 160 | } 161 | 162 | /** 163 | * Will return conditions on hourly basis for today 164 | * 165 | * @param type $latitude 166 | * @param type $longitude 167 | * @return \ForecastIOConditions|boolean 168 | */ 169 | public function getForecastToday($latitude, $longitude) 170 | { 171 | 172 | $data = $this->requestData($latitude, $longitude); 173 | 174 | if ($data !== false) { 175 | 176 | $conditions = array(); 177 | 178 | $today = date('Y-m-d'); 179 | 180 | foreach ($data->hourly->data as $raw_data) { 181 | 182 | if (date('Y-m-d', $raw_data->time) == $today) { 183 | 184 | $conditions[] = new ForecastIOConditions($raw_data); 185 | 186 | } 187 | 188 | } 189 | 190 | return $conditions; 191 | 192 | } else { 193 | 194 | return false; 195 | 196 | } 197 | 198 | } 199 | 200 | 201 | /** 202 | * Will return daily conditions for next seven days 203 | * 204 | * @param float $latitude 205 | * @param float $longitude 206 | * @return \ForecastIOConditions|boolean 207 | */ 208 | public function getForecastWeek($latitude, $longitude) 209 | { 210 | 211 | $data = $this->requestData($latitude, $longitude); 212 | 213 | if ($data !== false) { 214 | 215 | $conditions = array(); 216 | 217 | foreach ($data->daily->data as $raw_data) { 218 | 219 | $conditions[] = new ForecastIOConditions($raw_data); 220 | 221 | } 222 | 223 | return $conditions; 224 | 225 | } else { 226 | 227 | return false; 228 | 229 | } 230 | 231 | } 232 | 233 | 234 | } 235 | 236 | 237 | /** 238 | * Wrapper for get data by getters 239 | */ 240 | class ForecastIOConditions 241 | { 242 | 243 | private $raw_data; 244 | 245 | function __construct($raw_data) 246 | { 247 | 248 | $this->raw_data = $raw_data; 249 | 250 | } 251 | 252 | /** 253 | * Will return the temperature 254 | * 255 | * @return String 256 | */ 257 | function getTemperature() 258 | { 259 | 260 | return $this->raw_data->temperature; 261 | 262 | } 263 | 264 | /** 265 | * get the min temperature 266 | * 267 | * only available for week forecast 268 | * 269 | * @return type 270 | */ 271 | function getMinTemperature() 272 | { 273 | 274 | return $this->raw_data->temperatureMin; 275 | 276 | } 277 | 278 | /** 279 | * get max temperature 280 | * 281 | * only available for week forecast 282 | * 283 | * @return type 284 | */ 285 | function getMaxTemperature() 286 | { 287 | 288 | return $this->raw_data->temperatureMax; 289 | 290 | } 291 | 292 | /** 293 | * get apparent temperature (heat index/wind chill) 294 | * 295 | * only available for current conditions 296 | * 297 | * @return type 298 | */ 299 | function getApparentTemperature() 300 | { 301 | 302 | return $this->raw_data->apparentTemperature; 303 | 304 | } 305 | 306 | /** 307 | * Get the summary of the conditions 308 | * 309 | * @return String 310 | */ 311 | function getSummary() 312 | { 313 | 314 | return $this->raw_data->summary; 315 | 316 | } 317 | 318 | /** 319 | * Get the icon of the conditions 320 | * 321 | * @return String 322 | */ 323 | function getIcon() 324 | { 325 | 326 | return $this->raw_data->icon; 327 | 328 | } 329 | 330 | /** 331 | * Get the time, when $format not set timestamp else formatted time 332 | * 333 | * @param String $format 334 | * @return String 335 | */ 336 | function getTime($format = null) 337 | { 338 | 339 | if (!isset($format)) { 340 | 341 | return $this->raw_data->time; 342 | 343 | } else { 344 | 345 | return date($format, $this->raw_data->time); 346 | 347 | } 348 | 349 | } 350 | 351 | /** 352 | * Get the pressure 353 | * 354 | * @return String 355 | */ 356 | function getPressure() 357 | { 358 | 359 | return $this->raw_data->pressure; 360 | 361 | } 362 | 363 | /** 364 | * Get the dew point 365 | * 366 | * Available in the current conditions 367 | * 368 | * @return String 369 | */ 370 | function getDewPoint() 371 | { 372 | 373 | return $this->raw_data->dewPoint; 374 | 375 | } 376 | 377 | /** 378 | * get humidity 379 | * 380 | * @return String 381 | */ 382 | function getHumidity() 383 | { 384 | 385 | return $this->raw_data->humidity; 386 | 387 | } 388 | 389 | /** 390 | * Get the wind speed 391 | * 392 | * @return String 393 | */ 394 | function getWindSpeed() 395 | { 396 | 397 | return $this->raw_data->windSpeed; 398 | 399 | } 400 | 401 | /** 402 | * Get wind direction 403 | * 404 | * @return type 405 | */ 406 | function getWindBearing() 407 | { 408 | 409 | return $this->raw_data->windBearing; 410 | 411 | } 412 | 413 | /** 414 | * get precipitation type 415 | * 416 | * @return type 417 | */ 418 | function getPrecipitationType() 419 | { 420 | 421 | return $this->raw_data->precipType; 422 | 423 | } 424 | 425 | /** 426 | * get the probability 0..1 of precipitation type 427 | * 428 | * @return type 429 | */ 430 | function getPrecipitationProbability() 431 | { 432 | 433 | return $this->raw_data->precipProbability; 434 | 435 | } 436 | 437 | /** 438 | * Get the cloud cover 439 | * 440 | * @return type 441 | */ 442 | function getCloudCover() 443 | { 444 | 445 | return $this->raw_data->cloudCover; 446 | 447 | } 448 | 449 | 450 | /** 451 | * get sunrise time 452 | * 453 | * only available for week forecast 454 | * 455 | * @param String $format String to format date pph date 456 | * 457 | * @return type 458 | */ 459 | function getSunrise($format = null) 460 | { 461 | 462 | if (!isset($format)) { 463 | 464 | return $this->raw_data->sunriseTime; 465 | 466 | } else { 467 | 468 | return date($format, $this->raw_data->sunriseTime); 469 | 470 | } 471 | 472 | } 473 | 474 | /** 475 | * get sunset time 476 | * 477 | * only available for week forecast 478 | * 479 | * @param String $format String to format date pph date 480 | * 481 | * @return type 482 | */ 483 | function getSunset($format = null) 484 | { 485 | 486 | if (!isset($format)) { 487 | 488 | return $this->raw_data->sunsetTime; 489 | 490 | } else { 491 | 492 | return date($format, $this->raw_data->sunsetTime); 493 | 494 | } 495 | 496 | } 497 | 498 | } --------------------------------------------------------------------------------