mrcnpdlk/weather-api

Weather API and air quality for Poland

v1.0.1 2018-06-11 13:35 UTC

This package is auto-updated.

Last update: 2024-11-04 08:16:02 UTC


README

Contents

  1. Installation
  2. Basic usage
    1. Cache
    2. Logger
  3. External sources
    1. Chief Inspectorate of Environmental Protection
    2. Airly
    3. Open Weather Map
  4. API Usage
    1. Create instance
    2. Available methods
    3. Examples

Installation

Install the latest version with composer

composer require mrcnpdlk/weather-api

Basic usage

Cache

Library supports Cache bundles based on PSR-16 standard.

For below example was used phpfastcache/phpfastcache V7. phpfastcache/phpfastcache supports a lot of endpoints, i.e. Files, Sqlite, Redis and many other. More information about using cache and configuration it you can find in this Wiki.

    /**
     * Cache in system files
     */
    $oFilesConfig = new \Phpfastcache\Drivers\Files\Config();
    $oFilesConfig->setPath(sys_get_temp_dir())->setDefaultTtl(60);
    $oInstanceCacheFiles = new \Phpfastcache\Helper\Psr16Adapter('files', $oFilesConfig);
    /**
     * Cache in Redis
     */
    $oRedisConfig = new \Phpfastcache\Drivers\Redis\Config();
    $oRedisConfig
        ->setHost('localhost')
        ->setPort(6379)
        ->setDefaultTtl(60)
        ;
    $oInstanceCacheRedis = new \Phpfastcache\Helper\Psr16Adapter('redis', $oRedisConfig);

Log

Library also supports logging packages based on PSR-3 standard, i.e. very popular monolog/monolog.

$oInstanceLogger = new \Monolog\Logger('name_of_my_logger');
$oInstanceLogger->pushHandler(new \Monolog\Handler\ErrorLogHandler(
        \Monolog\Handler\ErrorLogHandler::OPERATING_SYSTEM,
        \Psr\Log\LogLevel::DEBUG
    )
)->pushProcessor(new \Monolog\Processor\PsrLogMessageProcessor());

External sources

GIOS

GIOS (Chief Inspectorate of Environmental Protection - Główny Inspektorat Ochrony Środowiska) provide API without any authentication.

Airly

Airly builds networks of air quality sensors that can be deployed across entire cities or counties. If you want to use external data using this API you have to get the free Token key from here .

OWM

Open Weather Map is well know project for weather and forecast. To use it, get the free Token Key from here .

API usage

Create instance

$oClient = new \mrcnpdlk\Weather\Client();
$oClient
    ->setCacheInstance($oInstanceCacheFiles)
    ->setLoggerInstance($oInstanceLogger)
    ->setAirlyConfig('AIRLY_TOKEN')  // not required but recommended
    ->setOWMConfig('OWM_TOKEN')  // not required but recommended
;
$oApi = new \mrcnpdlk\Weather\Api($oClient);

By default API uses:

  • current geo location found using public IP of machine where script is run
  • current date of machine where script is run

We are able to change location calling setLocation() method.

use mrcnpdlk\Weather\NativeModel\GeoPoint;

$oApi->setLocation(new GeoPoint(51.758158,19.544377));

Methods

Examples

getAddress()

mrcnpdlk\Weather\Model\Address Object
(
    [countryCode] => pl
    [province] => łódzkie
    [commune] => Łódź
    [place] => Łódź
    [neighbourhood] => Katedralna
    [street] => Wincentego Tymienieckiego
    [homeNr] => 16
    [postalCode] => 90-365
)

getSunSchedule()

Method can give inappropriate results for location near pole.

mrcnpdlk\Weather\Model\SunSchedule Object
(
    [sunrise] => 07:46:57
    [sunset] => 15:50:12
    [dayDuration] => 8:03
    [transit] => 11:48:34
    [civilTwilightBegin] => 07:07:27
    [civilTwilightEnd] => 16:29:42
    [nauticalTwilightBegin] => 06:24:41
    [nauticalTwilightEnd] => 17:12:27
    [astronomicalTwilightBegin] => 05:44:05
    [astronomicalTwilightEnd] => 17:53:03
)

getUVIndex()

0.43

getNearestGiosStation()

mrcnpdlk\Weather\NativeModel\Gios\Station Object
(
    [id] => 10058
    [stationName] => Łódź-Jana Pawła II 15
    [dateStart] => 2015-10-27 00:00:00
    [dateEnd] =>
    [gegrLat] => 51.754613
    [gegrLon] => 19.434925
    [addressStreet] => al. Jana Pawła II 15
    [city] => mrcnpdlk\Weather\NativeModel\Gios\City Object
        (
            [id] => 516
            [name] => Łódź
            [commune] => mrcnpdlk\Weather\NativeModel\Gios\Commune Object
                (
                    [communeName] => Łódź
                    [districtName] => Łódź
                    [provinceName] => ŁÓDZKIE
                )

        )

    [distance] => 2253.495
)

mrcnpdlk\Weather\NativeModel\Gios\Station has implemented two methods:

Native API