PHP code example of rugaard / weatherkit

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

    

rugaard / weatherkit example snippets


'providers' => [
    Rugaard\WeatherKit\Providers\LaravelServiceProvider::class,
]



use Rugaard\WeatherKit\Token;

// Details from "Create new App ID" section.
$appIdPrefix = 'O9876S4E2I';
$bundleId = 'com.forecast.weather';

// Details from "Create new key" section.
$pathToKey = 'credentials/AuthKey_I2E4S6789O.p8'
$keyId = 1234567890;

// Create Token instance.
$token = new Token($pathToKey, $keyId, $appIdPrefix, $bundleId);

// Method 1: getToken()
$accessToken = $token->getToken();

// Method 2: __toString()
$accessToken = (string) $token;



use Rugaard\WeatherKit\Client;

// Instantiate client without default location.
$weatherKit = new Client($token->getToken(), null, null, null);

// Instantiate client with default location.
$weatherKit = new Client((string) $token, 55.6736841, 12.5681471, 'dk');

// Instantiate client with default location and local language/timezone.
$weatherKit = new Client((string) $token, 55.6736841, 12.5681471, 'dk', 'da', 'Europe/Copenhagen');


// Change location to London.
$weatherKit->setLocation(51.5286416, -0.1015987);

// Country code is optional
// but ');

// Change timezone to Paris.
$weatherKit->setTimezone('Europe/Paris');

// Get all forecasts at once.
/* @var $forecasts \Illuminate\Support\Collection */
$forecasts = $weatherKit->weather();

// Get current forecast.
/* @var $forecast \Rugaard\WeatherKit\DTO\DataSets\Currently */
$forecast = $weatherKit->currently();

// Get next hour (minute-by-minute) forecast.
/* @var $forecast \Rugaard\WeatherKit\DTO\DataSets\NextHour */
$forecast = $weatherKit->nextHour();

// Get hourly forecast.
/* @var $forecast \Rugaard\WeatherKit\DTO\DataSets\Hourly */
$forecast = $weatherKit->hourly();

// Get daily forecast.
/* @var $forecast \Rugaard\WeatherKit\DTO\DataSets\Daily */
$forecast = $weatherKit->daily();

// Get weather alerts.
/* @var $alerts \Rugaard\WeatherKit\DTO\DataSets\Alerts */
$alerts = $weatherKit->alerts();

// Get detailed information about a specific alert.
/* @var $alertDetails \Rugaard\WeatherKit\DTO\Forecasts\AlertDetails */
$alertDetails = $weatherKit->alert($alerts->getData()->first()->getId());



namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use Rugaard\WeatherKit\Client as WeatherKit;

class WeatherController extends Controller
{
    /**
     * Get all forecasts at once.
     *
     * @param \Rugaard\WeatherKit\Client $weatherKit
     */
    public function forecast(WeatherKit $weatherKit)
    {
        $forecasts = $weatherKit->setLocation(55.6736841, 12.5681471, 'dk')->weather();
    }
    
    /**
     * Get current weather measurements.
     *
     * @param \Rugaard\WeatherKit\Client $weatherKit
     */
    public function currently(WeatherKit $weatherKit)
    {
        $forecasts = $weatherKit->setLocation(55.6736841, 12.5681471, 'dk')->currently();
    }
    
    /**
     * Get next hour (minute-by-minute) forecast.
     *
     * @param \Rugaard\WeatherKit\Client $weatherKit
     */
    public function nextHour(WeatherKit $weatherKit)
    {
        $forecasts = $weatherKit->setLocation(55.6736841, 12.5681471, 'dk')->nextHour();
    }
    
    /**
     * Get hourly forecast.
     *
     * @param \Rugaard\WeatherKit\Client $weatherKit
     */
    public function hourly(WeatherKit $weatherKit)
    {
        $forecasts = $weatherKit->setLocation(55.6736841, 12.5681471, 'dk')->hourly();
    }
    
    /**
     * Get daily forecast.
     *
     * @param \Rugaard\WeatherKit\Client $weatherKit
     */
    public function daily(WeatherKit $weatherKit)
    {
        $forecasts = $weatherKit->setLocation(55.6736841, 12.5681471, 'dk')->daily();
    }
}



namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Rugaard\WeatherKit\Client as WeatherKit;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(WeatherKit $weatherKit)
    {
        // Change location to London.
        $weatherKit->setLocation(51.5286416, -0.1015987);
        
        // Country code is optional
        // but 
shell
php artisan vendor:publish --provider=\Rugaard\WeatherKit\Providers\LaravelServiceProvider