1. Go to this page and download the library: Download horde/service_weather library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
horde / service_weather example snippets
use Horde\Service\Weather\Weather;
// Create service using Open-Meteo (free, unlimited, no API key)
$weather = Weather::openMeteo();
// Get current weather for Boston
$current = $weather->getCurrentWeather('42.3601,-71.0589');
echo "Temperature: " . $current->temperature->toCelsius() . "°C\n";
echo "Condition: " . $current->condition->getDescription() . "\n";
echo "Humidity: " . $current->humidity->format() . "\n";
// Get 5-day forecast
$forecast = $weather->getForecast('42.3601,-71.0589', days: 5);
foreach ($forecast->getPeriods() as $period) {
echo $period->date->format('Y-m-d') . ": ";
echo $period->condition->getDescription() . " ";
echo "High: " . $period->highTemperature->toFahrenheit() . "°F ";
echo "Low: " . $period->lowTemperature->toFahrenheit() . "°F\n";
}
// Requires API key from https://openweathermap.org/api
// Free tier: 1000 calls/day
$weather = Weather::openWeatherMap('your-api-key-here');
$current = $weather->getCurrentWeather('40.7128,-74.0060'); // New York
// Requires API key from https://www.weatherapi.com/
// Free tier: 1 million calls/month
$weather = Weather::weatherApi('your-api-key-here');
$forecast = $weather->getForecast('51.5074,-0.1278', days: 7); // London
// No API key ather::nationalWeatherService();
$current = $weather->getCurrentWeather('47.6062,-122.3321'); // Seattle
use Horde\Service\Weather\ValueObject\Location;
// From coordinates
$location = Location::fromCoordinates(48.8566, 2.3522);
// From coordinate object
$coordinate = Coordinate::fromLatLon(51.5074, -0.1278);
$location = Location::fromCoordinate($coordinate);
// From city name (some providers support this)
$location = Location::fromCity('Paris', 'France');
// Use with weather service
$current = $weather->getCurrentWeather($location);
use Horde\Service\Weather\Exception\ApiException;
use Horde\Service\Weather\Exception\InvalidApiKeyException;
use Horde\Service\Weather\Exception\InvalidLocationException;
try {
$current = $weather->getCurrentWeather($location);
} catch (InvalidApiKeyException $e) {
// API key is invalid or missing
} catch (InvalidLocationException $e) {
// Location format is invalid
} catch (ApiException $e) {
// General API error (network, rate limit, etc.)
}
use Horde\Http\Client;
$httpClient = new Client([
'timeout' => 30,
'userAgent' => 'MyApp/1.0',
]);
$weather = Weather::openMeteo($httpClient);
use Horde\Service\Weather\Provider\OpenMeteo;
use Horde\Http\Client;
use Horde\Service\Weather\ValueObject\WeatherConfig;
$provider = new OpenMeteo(
new Client(),
WeatherConfig::default()->withUnits(Units::IMPERIAL)
);
$current = $provider->getCurrentWeather('42.3601,-71.0589');
// Old PSR-0 API (still works)
$weather = Horde_Service_Weather::factory('Metar', [
'metar_path' => '/path/to/metar/files'
]);