|
4 | 4 | [](LICENSE) |
5 | 5 | [](https://github.com/programmatordev/openweathermap-php-api/actions/workflows/ci.yml?query=branch%3Amain) |
6 | 6 |
|
7 | | -OpenWeatherMap PHP library that provides convenient access to the OpenWeatherMap API. |
| 7 | +A fluent PHP client for OpenWeather APIs covering current and forecast weather, |
| 8 | +air pollution, geocoding, maps, stations, and One Call. Responses use typed |
| 9 | +entities that safely handle conditional, missing, and `null` data. |
8 | 10 |
|
9 | | -Supports [PSR-18 HTTP clients](https://www.php-fig.org/psr/psr-18), [PSR-17 HTTP factories](https://www.php-fig.org/psr/psr-17), [PSR-6 caches](https://www.php-fig.org/psr/psr-6) and [PSR-3 logs](https://www.php-fig.org/psr/psr-3). |
| 11 | +The library is built on |
| 12 | +[`programmatordev/php-api-sdk`](https://github.com/programmatordev/php-api-sdk), |
| 13 | +which provides HTTP client discovery and optional caching, logging, plugins, |
| 14 | +and request hooks. |
10 | 15 |
|
11 | 16 | ## Requirements |
12 | 17 |
|
13 | | -- PHP 8.1 or higher. |
14 | | - |
15 | | -## API Key |
16 | | - |
17 | | -A key is required to be able to make requests to the API. |
18 | | -You must sign up for an [OpenWeatherMap account](https://openweathermap.org/appid#signup) to get one. |
| 18 | +- PHP 8.1 or higher |
| 19 | +- An OpenWeather API key |
19 | 20 |
|
20 | 21 | ## Installation |
21 | 22 |
|
22 | | -Install the library via [Composer](https://getcomposer.org/): |
| 23 | +Install the library with Composer: |
23 | 24 |
|
24 | 25 | ```bash |
25 | 26 | composer require programmatordev/openweathermap-php-api |
26 | 27 | ``` |
27 | 28 |
|
28 | | -## Basic Usage |
| 29 | +## Getting Started |
| 30 | + |
| 31 | +Create the API client with an OpenWeather API key, then choose an API and call |
| 32 | +one of its methods: |
| 33 | + |
| 34 | +```php |
| 35 | +use ProgrammatorDev\OpenWeatherMap\OpenWeatherMap; |
| 36 | + |
| 37 | +$api = new OpenWeatherMap($_ENV['OPENWEATHERMAP_API_KEY']); |
| 38 | + |
| 39 | +$current = $api->weather()->current( |
| 40 | + latitude: 38.7223, |
| 41 | + longitude: -9.1393, |
| 42 | +); |
| 43 | + |
| 44 | +echo $current->temperature(); |
| 45 | +echo $current->temperatureWithUnit(); |
| 46 | +``` |
| 47 | + |
| 48 | +Response properties may be missing or `null`, so getters return nullable values |
| 49 | +where appropriate. Collection getters return empty arrays when the response |
| 50 | +does not contain that collection. |
29 | 51 |
|
30 | | -Simple usage looks like: |
| 52 | +## Configuration |
| 53 | + |
| 54 | +The client defaults to metric units and English. The equivalent explicit |
| 55 | +configuration is: |
31 | 56 |
|
32 | 57 | ```php |
| 58 | +use ProgrammatorDev\OpenWeatherMap\Enum\Language; |
| 59 | +use ProgrammatorDev\OpenWeatherMap\Enum\Units; |
33 | 60 | use ProgrammatorDev\OpenWeatherMap\OpenWeatherMap; |
34 | 61 |
|
35 | | -// initialize |
36 | | -$api = new OpenWeatherMap('yourapikey'); |
| 62 | +$api = new OpenWeatherMap( |
| 63 | + apiKey: $_ENV['OPENWEATHERMAP_API_KEY'], |
| 64 | + options: [ |
| 65 | + 'units' => Units::METRIC, |
| 66 | + 'language' => Language::ENGLISH, |
| 67 | + ], |
| 68 | +); |
| 69 | +``` |
| 70 | + |
| 71 | +Weather and One Call requests can override those values for one fluent request |
| 72 | +chain. The client-wide configuration remains unchanged for later requests: |
37 | 73 |
|
38 | | -// get current weather by coordinate (latitude, longitude) |
39 | | -$weather = $api->weather()->getCurrent(50, 50); |
40 | | -// show current temperature |
41 | | -echo $weather->getTemperature(); |
| 74 | +```php |
| 75 | +$current = $api |
| 76 | + ->weather() |
| 77 | + ->withUnits(Units::IMPERIAL) |
| 78 | + ->withLanguage(Language::PORTUGUESE) |
| 79 | + ->current(latitude: 38.7223, longitude: -9.1393); |
42 | 80 | ``` |
43 | 81 |
|
| 82 | +`withLanguage()` also accepts a non-empty language-code string, allowing new |
| 83 | +OpenWeather languages to be used without waiting for an enum update. |
| 84 | + |
| 85 | +See OpenWeather's |
| 86 | +[units of measurement](https://openweathermap.org/api/current?collection=current_forecast#data) and |
| 87 | +[multilingual support](https://openweathermap.org/api/current?collection=current_forecast#multi) |
| 88 | +documentation for the currently supported values. |
| 89 | + |
44 | 90 | ## Documentation |
45 | 91 |
|
46 | | -- [Usage](docs/01-usage.md) |
47 | | -- [Configuration](docs/02-configuration.md) |
48 | | -- [Supported APIs](docs/03-supported-apis.md) |
49 | | -- [Error Handling](docs/04-error-handling.md) |
50 | | -- [Entities](docs/05-entities.md) |
| 92 | +### APIs |
| 93 | + |
| 94 | +These guides cover each API's endpoints, response entities, and usage examples: |
| 95 | + |
| 96 | +- [One Call 4.0](docs/one-call.md) |
| 97 | +- [Air Pollution](docs/air-pollution.md) |
| 98 | +- [Weather](docs/weather.md) |
| 99 | +- [Maps](docs/maps.md) |
| 100 | +- [Stations](docs/stations.md) |
| 101 | +- [Geocoding](docs/geocoding.md) |
| 102 | + |
| 103 | +### Client Guides |
| 104 | + |
| 105 | +These guides cover client configuration and failures shared across the APIs: |
| 106 | + |
| 107 | +- [Setup](docs/setup.md) — Configure caching, logging, HTTP clients, plugins, |
| 108 | + and request hooks. |
| 109 | +- [Error Handling](docs/errors.md) — Handle OpenWeather API errors and client |
| 110 | + failures. |
51 | 111 |
|
52 | | -## Contributing |
| 112 | +## Upgrading |
53 | 113 |
|
54 | | -Any form of contribution to improve this library (including requests) will be welcome and appreciated. |
55 | | -Make sure to open a pull request or issue. |
| 114 | +Version 4 is a complete rewrite without backward compatibility. Existing |
| 115 | +integrations should treat it as a new implementation. See |
| 116 | +[Upgrading To 4.0](UPGRADE-4.0.md) for the release expectations and current |
| 117 | +baseline. |
56 | 118 |
|
57 | 119 | ## License |
58 | 120 |
|
59 | | -This project is licensed under the MIT license. |
60 | | -Please see the [LICENSE](LICENSE) file distributed with this source code for further information regarding copyright and licensing. |
| 121 | +This project is licensed under the [MIT License](LICENSE). |
0 commit comments