PHP code example of tuxonice / ipma-api

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

    

tuxonice / ipma-api example snippets


use Tlab\IpmaApi\IpmaForecast;

$api = IpmaForecast::createDailyWeatherForecastByLocalApi($cache);
$result = $api->from(1020500) // Location ID for Beja
              ->filterByMaxTemperatureRange(18.0, 19.0)
              ->get();

use Tlab\IpmaApi\IpmaObservation;
use Tlab\IpmaApi\Enums\SeismicInformationAreaEnum;

$api = IpmaObservation::createSeismicInformationApi($cache);
$events = $api->from(SeismicInformationAreaEnum::MAIN_LAND_AND_MADEIRA)
              ->get();

use Tlab\IpmaApi\IpmaService;

$api = IpmaService::createWeatherStationsApi($cache);
$stations = $api->filterByName('Lisboa', strict: false)->get();

use Tlab\IpmaApi\Enums\SeismicInformationAreaEnum;
use Tlab\IpmaApi\IpmaObservation;

$events = IpmaObservation::createSeismicInformationApi($cache)
    ->from(SeismicInformationAreaEnum::MAIN_LAND_AND_MADEIRA)
    ->filterByMagnitude(2.0, 5.0)
    ->get();

foreach ($events as $event) {
    echo $event->regionName, ' -> ', $event->magnitude, "\n"; // typed access
}

// Call ->toArray() on any DTO to recover the old array shape.
$legacy = $events[0]->toArray();

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;
use Tlab\IpmaApi\IpmaService;

$cache = new Psr16Cache(new FilesystemAdapter());

// Share the same $cache instance across every factory call:
$locations = IpmaService::createDistrictsIslandsLocationsApi($cache);
$stations  = IpmaService::createWeatherStationsApi($cache, ttlSeconds: 86400);

use Tlab\IpmaApi\ApiConnector;

$connector = new ApiConnector($cache, ttlSeconds: 3600);

use Tlab\IpmaApi\Exception\IpmaApiException;
use Tlab\IpmaApi\IpmaForecast;

try {
    $data = IpmaForecast::createDailyWeatherForecastByLocalApi($cache)->from(1020500)->get();
} catch (IpmaApiException $e) {
    // $e->getPrevious() returns the underlying Symfony exception, if any.
    error_log($e->getMessage());
}