|
4 | 4 | [](LICENSE) |
5 | 5 | [](https://github.com/programmatordev/openweathermap-php-api/actions/workflows/ci.yml?query=branch%3Amain) |
6 | 6 |
|
7 | | -OpenWeather PHP library built on |
8 | | -[`programmatordev/php-api-sdk`](https://github.com/programmatordev/php-api-sdk). |
| 7 | +A fluent PHP client for OpenWeather APIs covering current and forecast weather, |
| 8 | +air pollution, geocoding, maps, stations, and One Call. Responses are mapped to |
| 9 | +typed entities that safely handle conditional, missing, and `null` data while |
| 10 | +keeping common requests concise. |
9 | 11 |
|
10 | | -The library is currently under development and the public API is not ready for |
11 | | -use yet. |
| 12 | +The library is built on |
| 13 | +[`programmatordev/php-api-sdk`](https://github.com/programmatordev/php-api-sdk) |
| 14 | +and supports client-wide and request-local configuration. |
12 | 15 |
|
13 | 16 | ## Requirements |
14 | 17 |
|
15 | | -- PHP 8.1 or higher. |
| 18 | +- PHP 8.1 or higher |
| 19 | +- An OpenWeather API key |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Install the library with Composer: |
| 24 | + |
| 25 | +```bash |
| 26 | +composer require programmatordev/openweathermap-php-api |
| 27 | +``` |
| 28 | + |
| 29 | +## Getting Started |
| 30 | + |
| 31 | +Create the API client with an OpenWeather API key, then select a resource and |
| 32 | +endpoint: |
| 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 explicitly `null`, so entity getters |
| 49 | +return nullable values where appropriate. Collection getters return empty |
| 50 | +arrays when the corresponding response collection is absent or `null`. |
| 51 | + |
| 52 | +## Configuration |
| 53 | + |
| 54 | +The client defaults to metric units and English. The equivalent explicit |
| 55 | +configuration is: |
| 56 | + |
| 57 | +```php |
| 58 | +use ProgrammatorDev\OpenWeatherMap\Enum\Language; |
| 59 | +use ProgrammatorDev\OpenWeatherMap\Enum\Units; |
| 60 | +use ProgrammatorDev\OpenWeatherMap\OpenWeatherMap; |
| 61 | + |
| 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 | +Compatible resources can override those values for one fluent request chain. |
| 72 | +The original client configuration and other resource instances remain |
| 73 | +unchanged: |
| 74 | + |
| 75 | +```php |
| 76 | +$current = $api |
| 77 | + ->weather() |
| 78 | + ->withUnits(Units::IMPERIAL) |
| 79 | + ->withLanguage(Language::PORTUGUESE) |
| 80 | + ->current(latitude: 38.7223, longitude: -9.1393); |
| 81 | +``` |
| 82 | + |
| 83 | +`withLanguage()` also accepts a non-empty language-code string, allowing new |
| 84 | +OpenWeather languages to be used without waiting for an enum update. |
| 85 | + |
| 86 | +See OpenWeather's |
| 87 | +[units of measurement](https://openweathermap.org/api/current?collection=current_forecast#data) and |
| 88 | +[multilingual support](https://openweathermap.org/api/current?collection=current_forecast#multi) |
| 89 | +documentation for the currently supported values. |
16 | 90 |
|
17 | 91 | ## Documentation |
18 | 92 |
|
| 93 | +The detailed guides cover each API's endpoints, response entities, and usage |
| 94 | +examples: |
| 95 | + |
19 | 96 | - [One Call 4.0](docs/one-call.md) |
20 | 97 | - [Air Pollution](docs/air-pollution.md) |
21 | 98 | - [Weather](docs/weather.md) |
|
0 commit comments