PHP code example of ekstremedia / laravel-yr

1. Go to this page and download the library: Download ekstremedia/laravel-yr 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/ */

    

ekstremedia / laravel-yr example snippets


use Ekstremedia\LaravelYr\Services\WeatherHelper;

$helper = app(WeatherHelper::class);

// Get current weather by address
$result = $helper->getWeatherByAddress('Oslo, Norway');
// Returns: ['current' => [...], 'location' => [...]]

// Get current weather by coordinates
$result = $helper->getWeatherByCoordinates(59.9139, 10.7522, altitude: 90);

// Get forecast
$forecast = $helper->getForecastByAddress('Bergen, Norway');
$forecast = $helper->getForecastByCoordinates(60.39, 5.32, complete: true);

// Get sun data (sunrise/sunset)
$sun = $helper->getSunByAddress('Oslo, Norway');
$sun = $helper->getSunByCoordinates(59.9139, 10.7522, date: '2025-12-25', offset: 1);

// Get moon data (phase, rise/set)
$moon = $helper->getMoonByAddress('Bergen, Norway');
$moon = $helper->getMoonByCoordinates(60.39, 5.32, date: '2025-12-25', offset: 1);

use Ekstremedia\LaravelYr\Services\YrWeatherService;

$weather = app(YrWeatherService::class)->getCurrentWeather(59.9139, 10.7522);

return view('weather', ['weather' => $weather]);

return [
    // Required: User agent for MET Norway API
    'user_agent' => env('YR_USER_AGENT', 'YourApp/1.0 ([email protected])'),

    // Cache duration in seconds
    'cache_ttl' => env('YR_CACHE_TTL', 3600),

    // Enable/disable demo page at /yr
    'enable_demo_route' => env('YR_DEMO_ROUTE', true),

    // Enable/disable API routes
    'enable_api_routes' => env('YR_API_ROUTES', true),

    // Customize API route prefix (default: 'api/weather')
    'api_route_prefix' => env('YR_API_ROUTE_PREFIX', 'api/weather'),

    // Customize endpoint names
    'api_current_endpoint' => env('YR_API_CURRENT_ENDPOINT', 'current'),
    'api_forecast_endpoint' => env('YR_API_FORECAST_ENDPOINT', 'forecast'),
    'api_sun_endpoint' => env('YR_API_SUN_ENDPOINT', 'sun'),
    'api_moon_endpoint' => env('YR_API_MOON_ENDPOINT', 'moon'),
];
bash
php artisan vendor:publish --tag=yr-config
bash
php artisan vendor:publish --tag=yr-views
bash
php artisan vendor:publish --tag=yr-symbols