├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Cache.php ├── Http ├── Controllers │ └── LaravelOWMController.php └── routes.php ├── LaravelOWM.php ├── LaravelOWMServiceProvider.php └── config └── laravel-owm.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | /vendor 3 | /.idea 4 | /.vscode 5 | /.vagrant 6 | Homestead.json 7 | Homestead.yaml 8 | npm-debug.log 9 | yarn-error.log 10 | .phpunit.result.cache -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-OWM (Open Weather Map) 2 | ## A wrapper for [OpenWeatherMap-PHP-Api](https://github.com/cmfcmf/OpenWeatherMap-PHP-Api) writen by [@cmfcmf](https://github.com/cmfcmf) 3 | ### This package allows you to implement OpenWeatherMap-PHP-Api in laravel-way in your Laravel project. 4 | 5 | #### 1. Installation 6 | 7 | `composer require gmopx/laravel-owm` 8 | 9 | #### 2. Add this line to your conf/app.php file 10 | 11 | For Laravel == 5.0.* 12 | 13 | ``` 14 | 'Gmopx\LaravelOWM\LaravelOWMServiceProvider' 15 | ``` 16 | 17 | For Laravel <= 5.4.* 18 | 19 | ``` 20 | Gmopx\LaravelOWM\LaravelOWMServiceProvider::class, 21 | 22 | ``` 23 | 24 | For Laravel >= 5.5.* will use the auto-discovery function. 25 | 26 | #### 3. Publish the config file (config/laravel-owm.php) 27 | 28 | `php artisan vendor:publish --provider="Gmopx\LaravelOWM\LaravelOWMServiceProvider"` 29 | 30 | 31 | #### 4. Add your Open Weather Map API key 32 | 33 | ``` 34 | ... 35 | 'api_key' => '', // visit: http://openweathermap.org/appid#get for more info. 36 | 'routes_enabled' => true, // If the routes have to be enabled. 37 | ... 38 | ``` 39 | 40 | ### How to use... 41 | 42 | #### Current weather 43 | 44 | ``` 45 | ... 46 | use Gmopx\LaravelOWM\LaravelOWM; 47 | ... 48 | 49 | public function foo() 50 | { 51 | $lowm = new LaravelOWM(); 52 | $current_weather = $lowm->getCurrentWeather('london'); 53 | 54 | dd($current_weather->temperature); 55 | } 56 | 57 | ``` 58 | 59 | Visit [https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/CurrentWeather.php](https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/CurrentWeather.php) for more info. 60 | 61 | 62 | #### Forecast 63 | 64 | ``` 65 | ... 66 | use Gmopx\LaravelOWM\LaravelOWM; 67 | ... 68 | 69 | public function bar() 70 | { 71 | $lowm = new LaravelOWM(); 72 | $forecast = $lowm->getWeatherForecast('london'); 73 | 74 | dd($forecast); 75 | } 76 | 77 | ``` 78 | 79 | Visit [https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php](https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php) for more info. 80 | 81 | 82 | #### History 83 | 84 | ``` 85 | ... 86 | use Gmopx\LaravelOWM\LaravelOWM; 87 | ... 88 | 89 | public function bar() 90 | { 91 | $lowm = new LaravelOWM(); 92 | 93 | // Get yesterday's date 94 | $date = new \DateTime(); 95 | $date->add(\DateInterval::createFromDateString('yesterday')); 96 | 97 | $history = $lowm->getWeatherHistory('london', $date); 98 | 99 | dd($history); 100 | } 101 | 102 | ``` 103 | 104 | Visit [https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php](https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php) for more info. 105 | 106 | ##### Parameters: 107 | 108 | Note: 109 | ``` 110 | There are three ways to specify the place to get weather information for: 111 | - Use the city name: $query must be a string containing the city name. 112 | - Use the city id: $query must be an integer containing the city id. 113 | - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. 114 | ``` 115 | 116 | ##### getCurrentWeather: 117 | ``` 118 | /** 119 | * Get the current weather of the requested location/city. 120 | * 121 | * More info about how to interact with the results: 122 | * 123 | * https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/CurrentWeather.php 124 | * 125 | * @param $query (required) 126 | * @param string $lang (default: en) - http://openweathermap.org/current#multi. 127 | * @param string $units (default: metric) - 'metric' or 'imperial'. 128 | * @param bool $cache (default: false) 129 | * @param int $time (default: 600) 130 | * @return OpenWeatherMap\CurrentWeather 131 | */ 132 | public function getCurrentWeather($query, $lang = 'en', $units = 'metric', $cache = false, $time = 600) 133 | ... 134 | 135 | ``` 136 | 137 | ##### getWeatherForecast: 138 | ``` 139 | /** 140 | * Get the forecast of the requested location/city. 141 | * 142 | * More info about how to interact with the results: 143 | * 144 | * https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php 145 | * 146 | * @param $query (required) 147 | * @param string $lang (default: en) - http://openweathermap.org/current#multi. 148 | * @param string $units (default: metric) - 'metric' or 'imperial'. 149 | * @param int $days (default: 5) - maximum 16. 150 | * @param bool $cache (default: false) 151 | * @param int $time (default: 600) 152 | * @return OpenWeatherMap\WeatherForecast 153 | */ 154 | public function getWeatherForecast($query, $lang = 'en', $units = 'metric', $days = 5, $cache = false, $time = 600) 155 | ... 156 | 157 | ``` 158 | 159 | ##### getDailyWeatherForecast: 160 | ``` 161 | /** 162 | * Get the daily forecast of the requested location/city. 163 | * 164 | * 165 | * There are three ways to specify the place to get weather information for: 166 | * - Use the city name: $query must be a string containing the city name. 167 | * - Use the city id: $query must be an integer containing the city id. 168 | * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. 169 | * 170 | * @param array|int|string $query 171 | * @param string $lang 172 | * @param string $units 173 | * @param int $days 174 | * @param bool $cache 175 | * @param int $time 176 | * @return OpenWeatherMap\WeatherForecast 177 | */ 178 | public function getDailyWeatherForecast($query, $lang = 'en', $units = 'metric', $days = 5, $cache = false, $time = 600) 179 | ... 180 | ``` 181 | 182 | ##### getWeatherHistory: 183 | ``` 184 | /** 185 | * Get the forecast of the requested location/city. 186 | * 187 | * More info about how to interact with the results: 188 | * 189 | * https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherHistory.php 190 | * 191 | * @param $query (required) 192 | * @param \DateTime $start (default: today) 193 | * @param int $endOrCount (default: 1) 194 | * @param string $type (default: hour) - 'tick', 'hour', or 'day' 195 | * @param string $lang (default: en) - http://openweathermap.org/current#multi. 196 | * @param string $units (default: metric) - 'metric' or 'imperial'. 197 | * @param bool $cache (default: false) 198 | * @param int $time (default: 600) 199 | * @return OpenWeatherMap\WeatherForecast 200 | */ 201 | public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $lang = 'en', $units = 'metric', $cache = false, $time = 600) 202 | ... 203 | 204 | ``` 205 | 206 | 207 | ### Routes 208 | 209 | #### Additionally this package includes 2 routes ready-to-use that shows weather data in JSON format. 210 | 211 | `[GET]` `/current-weather` 212 | 213 | Parameters: 214 | ``` 215 | city: 'London' 216 | coord: ['lat': -0.13, 'lon': 51.51] 217 | lang: 'en' (default: 'en') 218 | units: 'metric'||'imperial' (default: 'metric') 219 | ``` 220 | 221 | `[GET]` `/forecast` 222 | 223 | Parameters: 224 | ``` 225 | city: 'London' 226 | coord: ['lat': -0.13, 'lon': 51.51] 227 | lang: 'en' (default: 'en') 228 | units: 'metric'||'imperial' (default: 'metric') 229 | days: 5 (default: 5) 230 | ``` 231 | 232 | Note: You must use `city` or `coord` but not both. 233 | 234 | 235 | #### /current-weather 236 | 237 | Consider the following: 238 | 239 | - The timezone for the dates of the results is taken from the `app/config.php` file ('app.timezone'). 240 | 241 | ``` 242 | { 243 | "status":"ok", 244 | "data":{ 245 | "city":{ 246 | "id":2643743, 247 | "name":"London", 248 | "lat":51.51, 249 | "lon":-0.13, 250 | "country":"GB", 251 | "population":null 252 | }, 253 | "sun":{ 254 | "rise":{ 255 | "date":"2016-09-14 05:34:37", 256 | "timestamp":1473831277 257 | }, 258 | "set":{ 259 | "date":"2016-09-14 18:16:08", 260 | "timestamp":1473876968 261 | } 262 | }, 263 | "lastUpdate":{ 264 | "date":"2016-09-14 03:58:17", 265 | "timestamp":1473825497 266 | }, 267 | "temperature":{ 268 | "now":{ 269 | "value":19.39, 270 | "unit":"°C" 271 | }, 272 | "min":{ 273 | "value":17, 274 | "unit":"°C" 275 | }, 276 | "max":{ 277 | "value":21.67, 278 | "unit":"°C" 279 | } 280 | }, 281 | "humidity":{ 282 | "value":82, 283 | "unit":"%" 284 | }, 285 | "pressure":{ 286 | "value":1008, 287 | "unit":"hPa" 288 | }, 289 | "wind":{ 290 | "speed":{ 291 | "value":2.1, 292 | "unit":"m/s", 293 | "description":"Light breeze", 294 | "description_slug":"light-breeze" 295 | }, 296 | "direction":{ 297 | "value":100, 298 | "unit":"E", 299 | "description":"East", 300 | "description_slug":"east" 301 | } 302 | }, 303 | "clouds":{ 304 | "value":8, 305 | "unit":"", 306 | "description":"clear sky", 307 | "description_slug":"clear-sky" 308 | }, 309 | "precipitation":{ 310 | "value":0, 311 | "unit":"", 312 | "description":"no", 313 | "description_slug":"no" 314 | }, 315 | "weather":{ 316 | "id":800, 317 | "description":"clear sky", 318 | "description_slug":"clear-sky", 319 | "icon":"02n" 320 | } 321 | } 322 | } 323 | 324 | ``` 325 | 326 | ### /forecast (5 days) 327 | 328 | Consider the following: 329 | 330 | - The timezone for the dates of the results is taken from the `app/config.php` file ('app.timezone'). 331 | - The indexes of the days array, are ISO-8601 numeric representation of the day of the week, 1 (for Monday) through 7 (for Sunday). 332 | - The OWM API returns 3 hour forecast data, it means for each day you requested you'll get weather data each 3 hours in this order: 333 | 06:00 - 09:00, 09:00 - 12:00, 12:00 - 15:00, 15:00 - 18:00, 18:00 - 21:00, 21:00 - 00:00. 334 | So to maintain a well-ordered info I built a key depending on the hours range (ie: ['06-09'] : { ... }). 335 | 336 | ``` 337 | { 338 | "status":"ok", 339 | "data":{ 340 | "city":{ 341 | "id":0, 342 | "name":"London", 343 | "lat":51.50853, 344 | "lon":-0.12574, 345 | "country":"GB", 346 | "population":null 347 | }, 348 | "sun":{ 349 | "rise":{ 350 | "date":"2016-09-14 05:34:37", 351 | "timestamp":1473831277 352 | }, 353 | "set":{ 354 | "date":"2016-09-14 18:16:05", 355 | "timestamp":1473876965 356 | } 357 | }, 358 | "lastUpdate":{ 359 | "date":"2016-09-14 04:17:23", 360 | "timestamp":1473826643 361 | }, 362 | "days":{ 363 | "3":{ 364 | "06-09":{ 365 | "time":{ 366 | "from":{ 367 | "date":"2016-09-14 06:00:00", 368 | "timestamp":1473832800 369 | }, 370 | "to":{ 371 | "date":"2016-09-14 09:00:00", 372 | "timestamp":1473843600 373 | }, 374 | "day":{ 375 | "date":"2016-09-14 00:00:00", 376 | "timestamp":1473811200 377 | } 378 | }, 379 | "lastUpdate":{ 380 | "date":"2016-09-14 04:17:23", 381 | "timestamp":1473826643 382 | }, 383 | "temperature":{ 384 | "now":{ 385 | "value":25.45, 386 | "unit":"°C" 387 | }, 388 | "min":{ 389 | "value":25.18, 390 | "unit":"°C" 391 | }, 392 | "max":{ 393 | "value":25.72, 394 | "unit":"°C" 395 | } 396 | }, 397 | "humidity":{ 398 | "value":53, 399 | "unit":"%" 400 | }, 401 | "pressure":{ 402 | "value":1016.12, 403 | "unit":"hPa" 404 | }, 405 | "wind":{ 406 | "speed":{ 407 | "value":3.12, 408 | "unit":"m\/s", 409 | "description":"Light breeze", 410 | "description_slug":"light-breeze" 411 | }, 412 | "direction":{ 413 | "value":111.003, 414 | "unit":"ESE", 415 | "description":"East-southeast", 416 | "description_slug":"east-southeast" 417 | } 418 | }, 419 | "clouds":{ 420 | "value":0, 421 | "unit":"%", 422 | "description":"clear sky", 423 | "description_slug":"clear-sky" 424 | }, 425 | "precipitation":{ 426 | "value":0, 427 | "unit":"", 428 | "description":"", 429 | "description_slug":"" 430 | }, 431 | "weather":{ 432 | "id":800, 433 | "description":"clear sky", 434 | "description_slug":"clear-sky", 435 | "icon":"01d" 436 | } 437 | }, 438 | 439 | ... 440 | }, 441 | "4":{ 442 | "03-06":{ 443 | "time":{ 444 | "from":{ 445 | "date":"2016-09-15 03:00:00", 446 | "timestamp":1473908400 447 | }, 448 | "to":{ 449 | "date":"2016-09-15 06:00:00", 450 | "timestamp":1473919200 451 | }, 452 | "day":{ 453 | "date":"2016-09-15 00:00:00", 454 | "timestamp":1473897600 455 | } 456 | }, 457 | "lastUpdate":{ 458 | "date":"2016-09-14 04:17:23", 459 | "timestamp":1473826643 460 | }, 461 | "temperature":{ 462 | "now":{ 463 | "value":16.34, 464 | "unit":"°C" 465 | }, 466 | "min":{ 467 | "value":16.34, 468 | "unit":"°C" 469 | }, 470 | "max":{ 471 | "value":16.34, 472 | "unit":"°C" 473 | } 474 | }, 475 | "humidity":{ 476 | "value":82, 477 | "unit":"%" 478 | }, 479 | "pressure":{ 480 | "value":1015.58, 481 | "unit":"hPa" 482 | }, 483 | "wind":{ 484 | "speed":{ 485 | "value":1.22, 486 | "unit":"m\/s", 487 | "description":"Calm", 488 | "description_slug":"calm" 489 | }, 490 | "direction":{ 491 | "value":40.0064, 492 | "unit":"NE", 493 | "description":"NorthEast", 494 | "description_slug":"northeast" 495 | } 496 | }, 497 | "clouds":{ 498 | "value":8, 499 | "unit":"%", 500 | "description":"clear sky", 501 | "description_slug":"clear-sky" 502 | }, 503 | "precipitation":{ 504 | "value":0, 505 | "unit":"", 506 | "description":"", 507 | "description_slug":"" 508 | }, 509 | "weather":{ 510 | "id":800, 511 | "description":"clear sky", 512 | "description_slug":"clear-sky", 513 | "icon":"02d" 514 | } 515 | }, 516 | ... 517 | }, 518 | "5":{ 519 | "03-06":{ 520 | "time":{ 521 | "from":{ 522 | "date":"2016-09-16 03:00:00", 523 | "timestamp":1473994800 524 | }, 525 | "to":{ 526 | "date":"2016-09-16 06:00:00", 527 | "timestamp":1474005600 528 | }, 529 | "day":{ 530 | "date":"2016-09-16 00:00:00", 531 | "timestamp":1473984000 532 | } 533 | }, 534 | "lastUpdate":{ 535 | "date":"2016-09-14 04:17:23", 536 | "timestamp":1473826643 537 | }, 538 | "temperature":{ 539 | "now":{ 540 | "value":17, 541 | "unit":"°C" 542 | }, 543 | "min":{ 544 | "value":17, 545 | "unit":"°C" 546 | }, 547 | "max":{ 548 | "value":17, 549 | "unit":"°C" 550 | } 551 | }, 552 | "humidity":{ 553 | "value":87, 554 | "unit":"%" 555 | }, 556 | "pressure":{ 557 | "value":1018.92, 558 | "unit":"hPa" 559 | }, 560 | "wind":{ 561 | "speed":{ 562 | "value":2.81, 563 | "unit":"m\/s", 564 | "description":"Light breeze", 565 | "description_slug":"light-breeze" 566 | }, 567 | "direction":{ 568 | "value":314.505, 569 | "unit":"NW", 570 | "description":"Northwest", 571 | "description_slug":"northwest" 572 | } 573 | }, 574 | "clouds":{ 575 | "value":88, 576 | "unit":"%", 577 | "description":"overcast clouds", 578 | "description_slug":"overcast-clouds" 579 | }, 580 | "precipitation":{ 581 | "value":0, 582 | "unit":"", 583 | "description":"", 584 | "description_slug":"" 585 | }, 586 | "weather":{ 587 | "id":804, 588 | "description":"overcast clouds", 589 | "description_slug":"overcast-clouds", 590 | "icon":"04d" 591 | } 592 | }, 593 | ... 594 | }, 595 | "6":{ 596 | "03-06":{ 597 | "time":{ 598 | "from":{ 599 | "date":"2016-09-17 03:00:00", 600 | "timestamp":1474081200 601 | }, 602 | "to":{ 603 | "date":"2016-09-17 06:00:00", 604 | "timestamp":1474092000 605 | }, 606 | "day":{ 607 | "date":"2016-09-17 00:00:00", 608 | "timestamp":1474070400 609 | } 610 | }, 611 | "lastUpdate":{ 612 | "date":"2016-09-14 04:17:23", 613 | "timestamp":1473826643 614 | }, 615 | "temperature":{ 616 | "now":{ 617 | "value":11.53, 618 | "unit":"°C" 619 | }, 620 | "min":{ 621 | "value":11.53, 622 | "unit":"°C" 623 | }, 624 | "max":{ 625 | "value":11.53, 626 | "unit":"°C" 627 | } 628 | }, 629 | "humidity":{ 630 | "value":91, 631 | "unit":"%" 632 | }, 633 | "pressure":{ 634 | "value":1029.85, 635 | "unit":"hPa" 636 | }, 637 | "wind":{ 638 | "speed":{ 639 | "value":2.91, 640 | "unit":"m\/s", 641 | "description":"Light breeze", 642 | "description_slug":"light-breeze" 643 | }, 644 | "direction":{ 645 | "value":314.505, 646 | "unit":"NW", 647 | "description":"Northwest", 648 | "description_slug":"northwest" 649 | } 650 | }, 651 | "clouds":{ 652 | "value":36, 653 | "unit":"%", 654 | "description":"scattered clouds", 655 | "description_slug":"scattered-clouds" 656 | }, 657 | "precipitation":{ 658 | "value":0, 659 | "unit":"", 660 | "description":"", 661 | "description_slug":"" 662 | }, 663 | "weather":{ 664 | "id":802, 665 | "description":"scattered clouds", 666 | "description_slug":"scattered-clouds", 667 | "icon":"03d" 668 | } 669 | }, 670 | ... 671 | }, 672 | "7":{ 673 | "03-06":{ 674 | "time":{ 675 | "from":{ 676 | "date":"2016-09-18 03:00:00", 677 | "timestamp":1474167600 678 | }, 679 | "to":{ 680 | "date":"2016-09-18 06:00:00", 681 | "timestamp":1474178400 682 | }, 683 | "day":{ 684 | "date":"2016-09-18 00:00:00", 685 | "timestamp":1474156800 686 | } 687 | }, 688 | "lastUpdate":{ 689 | "date":"2016-09-14 04:17:23", 690 | "timestamp":1473826643 691 | }, 692 | "temperature":{ 693 | "now":{ 694 | "value":11.44, 695 | "unit":"°C" 696 | }, 697 | "min":{ 698 | "value":11.44, 699 | "unit":"°C" 700 | }, 701 | "max":{ 702 | "value":11.44, 703 | "unit":"°C" 704 | } 705 | }, 706 | "humidity":{ 707 | "value":98, 708 | "unit":"%" 709 | }, 710 | "pressure":{ 711 | "value":1031.08, 712 | "unit":"hPa" 713 | }, 714 | "wind":{ 715 | "speed":{ 716 | "value":2.96, 717 | "unit":"m\/s", 718 | "description":"Light breeze", 719 | "description_slug":"light-breeze" 720 | }, 721 | "direction":{ 722 | "value":240.011, 723 | "unit":"WSW", 724 | "description":"West-southwest", 725 | "description_slug":"west-southwest" 726 | } 727 | }, 728 | "clouds":{ 729 | "value":0, 730 | "unit":"%", 731 | "description":"clear sky", 732 | "description_slug":"clear-sky" 733 | }, 734 | "precipitation":{ 735 | "value":0, 736 | "unit":"", 737 | "description":"", 738 | "description_slug":"" 739 | }, 740 | "weather":{ 741 | "id":800, 742 | "description":"clear sky", 743 | "description_slug":"clear-sky", 744 | "icon":"01d" 745 | } 746 | }, 747 | ... 748 | } 749 | } 750 | } 751 | } 752 | ``` 753 | 754 | ## Changelog 755 | 756 | v0.1.2 757 | - Support for getDailyWeatherForecast 758 | 759 | v0.1.1 760 | - Whitespace/formatting. 761 | - Added getWeatherHistory method 762 | - Additional README documentation 763 | 764 | Thanks to: 765 | - [@nateritter](https://github.com/nateritter) 766 | - [@jmaurer1994](https://github.com/jmaurer1994) 767 | 768 | ### License: MIT 769 | 770 | 771 | ### Contributing 772 | You can help me fixing my horrific English in the documentation & comments. 773 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gmopx/laravel-owm", 3 | "description": "Wrapper for cmfcmf/openweathermap-php-api", 4 | "keywords": ["weather","clima","open weather map","laravel"], 5 | "require": { 6 | "php": ">=5.4.0", 7 | "cmfcmf/openweathermap-php-api": "^2.1" 8 | }, 9 | 10 | "license": "MIT", 11 | 12 | "authors": [ 13 | { 14 | "name": "Guillermo Palafox", 15 | "email": "palafox.87@gmail.com" 16 | } 17 | ], 18 | "minimum-stability": "dev", 19 | 20 | "autoload": { 21 | "psr-4": { 22 | "Gmopx\\LaravelOWM\\": "src/" 23 | } 24 | }, 25 | 26 | "extra": { 27 | "laravel": { 28 | "providers": [ 29 | "Gmopx\\LaravelOWM\\LaravelOWMServiceProvider" 30 | ] 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Cache.php: -------------------------------------------------------------------------------- 1 | storagePath() . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI"; 12 | if (!is_dir($dir)) { 13 | mkdir($dir); 14 | } 15 | $path = $dir . DIRECTORY_SEPARATOR . md5($url); 16 | return $path; 17 | } 18 | 19 | /** 20 | * Checks whether a cached weather data is available. 21 | * 22 | * @param string $url The unique url of the cached content. 23 | * 24 | * @return bool False if no cached information is available, otherwise true. 25 | * 26 | * You need to check if a cached result is outdated here. Return false in that case. 27 | */ 28 | public function isCached($url) 29 | { 30 | $path = $this->urlToPath($url); 31 | 32 | if (!file_exists($path) || filectime($path) + $this->seconds < time()) { 33 | return false; 34 | } 35 | 36 | return true; 37 | } 38 | 39 | /** 40 | * Returns cached weather data. 41 | * 42 | * @param string $url The unique url of the cached content. 43 | * 44 | * @return string|bool The cached data if it exists, false otherwise. 45 | */ 46 | public function getCached($url) 47 | { 48 | return file_get_contents($this->urlToPath($url)); 49 | } 50 | 51 | /** 52 | * Saves cached weather data. 53 | * 54 | * @param string $url The unique url of the cached content. 55 | * @param string $content The weather data to cache. 56 | * 57 | * @return bool True on success, false on failure. 58 | */ 59 | public function setCached($url, $content) 60 | { 61 | file_put_contents($this->urlToPath($url), $content); 62 | } 63 | } -------------------------------------------------------------------------------- /src/Http/Controllers/LaravelOWMController.php: -------------------------------------------------------------------------------- 1 | get('city'); 23 | $coordinates = $request->get('coord'); 24 | $lang = $request->get('lang', 'en'); 25 | $units = $request->get('units', 'metric'); 26 | 27 | if ($city === null && $coordinates == null) { 28 | abort('400','City or coordinates cannot be undefined.'); 29 | } 30 | 31 | $query = ($city) ?: $coordinates; 32 | 33 | try { 34 | $current_weather = $lowm->getCurrentWeather($query, $lang, $units, true); 35 | } catch(\Exception $e) { 36 | return response()->json(['status' => 'error', 'message' => $e->getMessage(), 'code' => $e->getCode()]); 37 | } 38 | 39 | $data = [ 40 | 'city' => [ 41 | 'id' => $current_weather->city->id, 42 | 'name' => $current_weather->city->name, 43 | 'lat' => $current_weather->city->lat, 44 | 'lon' => $current_weather->city->lon, 45 | 'country' => $current_weather->city->country, 46 | 'population' => $current_weather->city->population 47 | ], 48 | 'sun' => [ 49 | 'rise' => [ 50 | 'date' => $current_weather->sun->rise->setTimezone($tz)->format('Y-m-d H:i:s'), 51 | 'timestamp' => $current_weather->sun->rise->setTimezone($tz)->getTimestamp() 52 | ], 53 | 'set' => [ 54 | 'date' => $current_weather->sun->set->setTimezone($tz)->format('Y-m-d H:i:s'), 55 | 'timestamp' => $current_weather->sun->set->setTimezone($tz)->getTimestamp() 56 | ] 57 | ], 58 | 'lastUpdate' => [ 59 | 'date' => $current_weather->lastUpdate->setTimezone($tz)->format('Y-m-d H:i:s'), 60 | 'timestamp' => $current_weather->lastUpdate->setTimezone($tz)->getTimestamp() 61 | ] 62 | ]; 63 | 64 | $data = array_merge($data, $this->parseData($current_weather)); 65 | 66 | return response()->json(['status' => 'ok', 'data' => $data]); 67 | } 68 | 69 | /** 70 | * Response with the forecast of the requested location/city. 71 | * 72 | * @param Request $request 73 | * @return \Symfony\Component\HttpFoundation\Response 74 | */ 75 | public function forecast(Request $request) 76 | { 77 | $lowm = new LaravelOWM(); 78 | $tz = new \DateTimeZone(config('app.timezone')); 79 | 80 | $city = $request->get('city'); 81 | $coordinates = $request->get('coord'); 82 | $lang = $request->get('lang', 'en'); 83 | $units = $request->get('units', 'metric'); 84 | $days = $request->get('days', 5); 85 | 86 | if ($city === null && $coordinates == null) { 87 | abort('400','City or coordinates cannot be undefined.'); 88 | } 89 | 90 | $query = ($city) ?: $coordinates; 91 | 92 | try { 93 | $forecast = $lowm->getWeatherForecast($query, $lang, $units, $days, true); 94 | } catch(\Exception $e){ 95 | return response()->json(['status' => 'error', 'message' => $e->getMessage(), 'code' => $e->getCode()]); 96 | } 97 | 98 | $data = [ 99 | 'city' => [ 100 | 'id' => $forecast->city->id, 101 | 'name' => $forecast->city->name, 102 | 'lat' => $forecast->city->lat, 103 | 'lon' => $forecast->city->lon, 104 | 'country' => $forecast->city->country, 105 | 'population' => $forecast->city->population 106 | ], 107 | 'sun' => [ 108 | 'rise' => [ 109 | 'date' => $forecast->sun->rise->setTimezone($tz)->format('Y-m-d H:i:s'), 110 | 'timestamp' => $forecast->sun->rise->setTimezone($tz)->getTimestamp() 111 | ], 112 | 'set' => [ 113 | 'date' => $forecast->sun->set->setTimezone($tz)->format('Y-m-d H:i:s'), 114 | 'timestamp' => $forecast->sun->set->setTimezone($tz)->getTimestamp() 115 | ] 116 | ], 117 | 'lastUpdate' => [ 118 | 'date' => $forecast->lastUpdate->setTimezone($tz)->format('Y-m-d H:i:s'), 119 | 'timestamp' => $forecast->lastUpdate->setTimezone($tz)->getTimestamp() 120 | ] 121 | ]; 122 | 123 | foreach ($forecast as $obj) { 124 | 125 | $day = $obj->time->day->setTimezone($tz); 126 | $from = $obj->time->from->setTimezone($tz); 127 | $to = $obj->time->to->setTimezone($tz); 128 | 129 | $temp = [ 130 | 131 | 'time' => [ 132 | 'from' => [ 133 | 'date' => $from->format('Y-m-d H:i:s'), 134 | 'timestamp' => $from->getTimestamp() 135 | ], 136 | 'to' => [ 137 | 'date' => $to->format('Y-m-d H:i:s'), 138 | 'timestamp' => $to->getTimestamp() 139 | ], 140 | 'day' => [ 141 | 'date' => $day->format('Y-m-d H:i:s'), 142 | 'timestamp' => $day->getTimestamp() 143 | ], 144 | ], 145 | 146 | 'lastUpdate' => [ 147 | 'date' => $obj->lastUpdate->setTimezone($tz)->format('Y-m-d H:i:s'), 148 | 'timestamp' => $obj->lastUpdate->setTimezone($tz)->getTimestamp() 149 | ] 150 | ]; 151 | 152 | if (isset($last_day)) { 153 | 154 | if($day->format('Y-m-d H:i:s') == $last_day){ 155 | 156 | // ISO-8601 numeric representation of the day of the week 157 | // 1 (for Monday) through 7 (for Sunday) 158 | $day_key = $day->format('N'); 159 | 160 | // The OWM API returns 3 hour forecast data, it means for each day you requested you'll 161 | // get weather data each 3 hours in this order: 162 | // 06:00 - 09:00, 09:00 - 12:00, 12:00 - 15:00, 15:00 - 18:00, 18:00 - 21:00, 21:00 - 00:00. 163 | // So to maintain a well-ordered info I built a key depending on the hours range 164 | // (ie: ['06-09'] => [ ... ]). 165 | $time_key = $from->format('H').'-'.$to->format('H'); 166 | 167 | $data['days'][$day_key][$time_key] = array_merge($temp, $this->parseData($obj)); 168 | } 169 | } 170 | 171 | $last_day = $day->format('Y-m-d H:i:s'); 172 | } 173 | 174 | return response()->json(['status' => 'ok', 'data' => $data]); 175 | } 176 | 177 | /** 178 | * Helper function to parse data. 179 | * 180 | * @param $obj 181 | * @return array 182 | */ 183 | private function parseData($obj) 184 | { 185 | $data = [ 186 | 'temperature' => [ 187 | 'now' => [ 188 | 'value' => $obj->temperature->now->getValue(), 189 | 'unit' => $obj->temperature->now->getUnit() 190 | ], 191 | 'min' => [ 192 | 'value' => $obj->temperature->min->getValue(), 193 | 'unit' => $obj->temperature->min->getUnit() 194 | ], 195 | 'max' => [ 196 | 'value' => $obj->temperature->max->getValue(), 197 | 'unit' => $obj->temperature->max->getUnit() 198 | ] 199 | ], 200 | 'humidity' => [ 201 | 'value' => $obj->humidity->getValue(), 202 | 'unit' => $obj->humidity->getUnit() 203 | ], 204 | 'pressure' => [ 205 | 'value' => $obj->pressure->getValue(), 206 | 'unit' => $obj->pressure->getUnit() 207 | ], 208 | 'wind' => [ 209 | 'speed' => [ 210 | 'value' => $obj->wind->speed->getValue(), 211 | 'unit' => $obj->wind->speed->getUnit(), 212 | 'description' => $obj->wind->speed->getDescription(), 213 | 'description_slug' => str_slug($obj->wind->speed->getDescription()) 214 | ], 215 | 'direction' => [ 216 | 'value' => $obj->wind->direction->getValue(), 217 | 'unit' => $obj->wind->direction->getUnit(), 218 | 'description' => $obj->wind->direction->getDescription(), 219 | 'description_slug' => str_slug($obj->wind->direction->getDescription()) 220 | ] 221 | ], 222 | 'clouds' => [ 223 | 'value' => $obj->clouds->getValue(), 224 | 'unit' => $obj->clouds->getUnit(), 225 | 'description' => $obj->clouds->getDescription(), 226 | 'description_slug' => str_slug($obj->clouds->getDescription()) 227 | ], 228 | 'precipitation' => [ 229 | 'value' => $obj->precipitation->getValue(), 230 | 'unit' => $obj->precipitation->getUnit(), 231 | 'description' => $obj->precipitation->getDescription(), 232 | 'description_slug' => str_slug($obj->precipitation->getDescription()) 233 | ], 234 | 'weather' => [ 235 | 'id' => $obj->weather->id, 236 | 'description' => $obj->weather->description, 237 | 'description_slug' => str_slug($obj->weather->description), 238 | 'icon' => $obj->weather->icon 239 | ], 240 | ]; 241 | 242 | return $data; 243 | } 244 | 245 | } 246 | -------------------------------------------------------------------------------- /src/Http/routes.php: -------------------------------------------------------------------------------- 1 | 'owmapi', 'namespace' => 'Gmopx\LaravelOWM\Http\Controllers'], function() { 4 | 5 | Route::get('current-weather', ['uses' => 'LaravelOWMController@currentweather']); 6 | Route::get('forecast', ['uses' => 'LaravelOWMController@forecast']); 7 | 8 | }); 9 | -------------------------------------------------------------------------------- /src/LaravelOWM.php: -------------------------------------------------------------------------------- 1 | config = config('laravel-owm'); 21 | 22 | if ($this->config === null) { 23 | throw new \Exception('config/laravel-owm.php not found'); 24 | } 25 | 26 | if ($this->config['api_key'] === null) { 27 | throw new \Exception('laravel-owm.api_key not found'); 28 | } 29 | 30 | $this->api_key = $this->config['api_key']; 31 | } 32 | 33 | /** 34 | * Get the current weather of the requested location/city. 35 | * 36 | * More info about how to interact with the results: 37 | * 38 | * https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/CurrentWeather.php 39 | * 40 | * There are three ways to specify the place to get weather information for 41 | * - Use the city name: $query must be a string containing the city name. 42 | * - Use the city id: $query must be an integer containing the city id. 43 | * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. 44 | * 45 | * @param array|int|string $query 46 | * @param string $lang 47 | * @param string $units 48 | * @param bool $cache 49 | * @param int $time 50 | * @return OpenWeatherMap\CurrentWeather 51 | */ 52 | public function getCurrentWeather($query, $lang = 'en', $units = 'metric', $cache = false, $time = 600) 53 | { 54 | $lang = $lang ?: 'en'; 55 | $units = $units ?: 'metric'; 56 | 57 | if ($cache) { 58 | $owm = new OpenWeatherMap($this->api_key, null, new Cache(), $time); 59 | return $owm->getWeather($query, $units, $lang); 60 | } 61 | 62 | $owm = new OpenWeatherMap($this->api_key); 63 | return $owm->getWeather($query, $units, $lang); 64 | } 65 | 66 | /** 67 | * Get the forecast of the requested location/city. 68 | * 69 | * More info about how to interact with the results: 70 | * 71 | * https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherForecast.php 72 | * 73 | * There are three ways to specify the place to get weather information for: 74 | * - Use the city name: $query must be a string containing the city name. 75 | * - Use the city id: $query must be an integer containing the city id. 76 | * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. 77 | * 78 | * @param array|int|string $query 79 | * @param string $lang 80 | * @param string $units 81 | * @param int $days 82 | * @param bool $cache 83 | * @param int $time 84 | * @return OpenWeatherMap\WeatherForecast 85 | */ 86 | public function getWeatherForecast($query, $lang = 'en', $units = 'metric', $days = 5, $cache = false, $time = 600) 87 | { 88 | $lang = $lang ?: 'en'; 89 | $units = $units ?: 'metric'; 90 | $days = $days ?: 6; 91 | 92 | if ($cache) { 93 | $owm = new OpenWeatherMap($this->api_key, null, new Cache(), $time); 94 | return $owm->getWeatherForecast($query, $units, $lang, '', $days); 95 | } 96 | 97 | $owm = new OpenWeatherMap($this->api_key); 98 | return $owm->getWeatherForecast($query, $units, $lang, '', $days); 99 | } 100 | 101 | /** 102 | * Get the daily forecast of the requested location/city. 103 | * 104 | * 105 | * There are three ways to specify the place to get weather information for: 106 | * - Use the city name: $query must be a string containing the city name. 107 | * - Use the city id: $query must be an integer containing the city id. 108 | * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. 109 | * 110 | * @param array|int|string $query 111 | * @param string $lang 112 | * @param string $units 113 | * @param int $days 114 | * @param bool $cache 115 | * @param int $time 116 | * @return OpenWeatherMap\WeatherForecast 117 | */ 118 | public function getDailyWeatherForecast($query, $lang = 'en', $units = 'metric', $days = 5, $cache = false, $time = 600) 119 | { 120 | $lang = $lang ?: 'en'; 121 | $units = $units ?: 'metric'; 122 | $days = $days ?: 6; 123 | 124 | if ($cache) { 125 | $owm = new OpenWeatherMap($this->api_key, null, new Cache(), $time); 126 | return $owm->getDailyWeatherForecast($query, $units, $lang, '', $days); 127 | } 128 | 129 | $owm = new OpenWeatherMap($this->api_key); 130 | return $owm->getDailyWeatherForecast($query, $units, $lang, '', $days); 131 | } 132 | 133 | /** 134 | * Returns the weather history for the place you specified. 135 | * 136 | * More info about how to interact with the results: 137 | * 138 | * https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/Examples/WeatherHistory.php 139 | * 140 | * There are three ways to specify the place to get weather information for: 141 | * - Use the city name: $query must be a string containing the city name. 142 | * - Use the city id: $query must be an integer containing the city id. 143 | * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. 144 | * 145 | * @param array|int|string $query 146 | * @param \DateTime $start 147 | * @param int $endOrCount 148 | * @param string $type 149 | * @param string $lang 150 | * @param string $units 151 | * @param bool $cache 152 | * @param int $time 153 | * @return OpenWeatherMap\WeatherHistory 154 | */ 155 | public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $lang = 'en', $units = 'metric', $cache = false, $time = 600) 156 | { 157 | $lang = $lang ?: 'en'; 158 | $units = $units ?: 'metric'; 159 | $start = $start ?: new \DateTime; 160 | $endOrCount = $endOrCount ?: 1; 161 | $type = $type ?: 'hour'; 162 | 163 | if ($cache) { 164 | $owm = new OpenWeatherMap($this->api_key, null, new Cache(), $time); 165 | return $owm->getWeatherHistory($query, $start, $endOrCount, $type, $units, $lang, ''); 166 | } 167 | 168 | $owm = new OpenWeatherMap($this->api_key); 169 | return $owm->getWeatherHistory($query, $start, $endOrCount, $type, $units, $lang, ''); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/LaravelOWMServiceProvider.php: -------------------------------------------------------------------------------- 1 | app['config']->get('laravel-owm'); 16 | 17 | // Loading routes file 18 | if ($config !== null) { 19 | 20 | if ($config['routes_enabled']) { 21 | require __DIR__ . '/Http/routes.php'; 22 | } 23 | 24 | } else { 25 | require __DIR__ . '/Http/routes.php'; 26 | } 27 | 28 | $this->publishes([ 29 | __DIR__ . '/config' => config_path() 30 | ]); 31 | 32 | } 33 | 34 | /** 35 | * Register the application services. 36 | * 37 | * @return void 38 | */ 39 | public function register() 40 | { 41 | $this->app->bind('Gmopx\LaravelOWM\LaravelOWM', function(){ 42 | 43 | return new \Gmopx\LaravelOWM\LaravelOWM(); 44 | 45 | }); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/config/laravel-owm.php: -------------------------------------------------------------------------------- 1 | '', // visit: http://openweathermap.org/appid#get for more info. 5 | 'routes_enabled' => true, // If the routes have to be enabled. 6 | ]; 7 | --------------------------------------------------------------------------------