2 Commits

Author SHA1 Message Date
Oh
e47cc0122c Merge pull request 'Update Readme' (#2) from package into master
Some checks failed
Tests / Tests PHP 7.4 (push) Has been cancelled
Tests / Tests PHP 8 (push) Has been cancelled
Tests / Tests PHP 8.1 (push) Has been cancelled
Reviewed-on: #2
2025-09-05 08:45:32 +00:00
Oh
f9247aa54c Update Readme
Some checks failed
Tests / Tests PHP 7.4 (push) Has been cancelled
Tests / Tests PHP 8 (push) Has been cancelled
Tests / Tests PHP 8.1 (push) Has been cancelled
Tests / Tests PHP 7.4 (pull_request) Has been cancelled
Tests / Tests PHP 8 (pull_request) Has been cancelled
Tests / Tests PHP 8.1 (pull_request) Has been cancelled
2025-09-05 10:44:47 +02:00

111
README.md
View File

@@ -1,42 +1,99 @@
# Slim Framework 4 Skeleton Application ## Using the OpenWeatherClient Library
[![Coverage Status](https://coveralls.io/repos/github/slimphp/Slim-Skeleton/badge.svg?branch=master)](https://coveralls.io/github/slimphp/Slim-Skeleton?branch=master) To integrate weather data into your application using the `ohrionmartin/weather` package, follow these steps:
Use this skeleton application to quickly setup and start working on a new Slim Framework 4 application. This application uses the latest Slim 4 with Slim PSR-7 implementation and PHP-DI container implementation. It also uses the Monolog logger. ### 1. Add the Repository and Dependency
This skeleton application was built for Composer. This makes setting up a new Slim Framework application quick and easy. Update your `composer.json` file to include the custom repository and require the `ohrionmartin/weather` package:
## Install the Application ```json
"repositories": [
Run this command from the directory in which you want to install your new Slim Framework application. You will require PHP 7.4 or newer. {
"type": "vcs",
```bash "url": "https://git.nampharm.com.na/ohrionmartin/weather"
composer create-project slim/slim-skeleton [my-app-name] }
],
"require": {
"ohrionmartin/weather": "dev-master"
}
``` ```
Replace `[my-app-name]` with the desired directory name for your new application. You'll want to: Then run:
* Point your virtual host document root to your new application's `public/` directory.
* Ensure `logs/` is web writable.
To run the application in development, you can run these commands
```bash ```bash
cd [my-app-name] composer update
composer start
``` ```
Or you can use `docker-compose` to run the app with `docker`, so you can run these commands: ### 2. Configure the API Key
```bash
cd [my-app-name]
docker-compose up -d
```
After that, open `http://localhost:8080` in your browser.
Run this command in the application directory to run the test suite Ensure you have an API key from OpenWeatherMap. Add it to your `.env` file:
```bash ```bash
composer test OPENWEATHER_API_KEY=your_api_key_here
``` ```
That's it! Now go build something cool. ### 3. Add Weather Route
In your application (e.g., in `routes.php` or equivalent), add the following route to handle weather requests:
```php
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Ohrionmartin\Weather\Service\OpenWeatherClient;
$router->get('/weather', function (Request $request) {
$apiKey = env('OPENWEATHER_API_KEY');
if (!$apiKey) {
return response()->json(['error' => 'OPENWEATHER_API_KEY is not configured'], 500);
}
$client = new Client();
$ow = new OpenWeatherClient($client, $apiKey);
$options = [];
if ($request->has('units')) {
$options['units'] = $request->get('units'); // 'standard'|'metric'|'imperial'
}
if ($request->has('lang')) {
$options['lang'] = $request->get('lang');
}
if ($request->has('exclude')) {
$options['exclude'] = $request->get('exclude'); // comma-separated segments
}
try {
if ($request->has('city')) {
$data = $ow->oneCallByCity((string) $request->get('city'), $options);
} else {
if (!$request->has('lat') || !$request->has('lon')) {
return response()->json(['error' => 'Provide either ?city=Name or ?lat=..&lon=..'], 400);
}
$lat = (float) $request->get('lat');
$lon = (float) $request->get('lon');
$data = $ow->oneCall($lat, $lon, $options);
}
return response()->json($data);
} catch (\RuntimeException $e) {
return response()->json(['error' => $e->getMessage()], 502);
}
});
```
### 4. Usage Examples
You can access the weather endpoint with the following query parameters:
- **By city name**: `http://localhost:8080/weather?city=London`
- **By coordinates**: `http://localhost:8080/weather?lat=51.5074&lon=-0.1278`
- **With optional parameters**:
- `units`: `standard`, `metric`, or `imperial` (e.g., `?units=metric`)
- `lang`: Language code for response (e.g., `?lang=fr`)
- `exclude`: Comma-separated segments to exclude (e.g., `?exclude=minutely,hourly`)
Example request:
```bash
curl "http://localhost:8080/weather?city=London&units=metric&lang=fr"
```
This will return weather data in JSON format for London in metric units and French language.