adamale/nbpfetch

PHP library that helps to fetch data from NBP API.

1.0 2019-08-30 17:58 UTC

This package is auto-updated.

Last update: 2020-09-08 11:34:15 UTC


README

PHP library that helps to fetch data from NBP API - http://api.nbp.pl/en.html.
It allows you to fetch currency rates, exchange rate tables and gold prices.

Requirements

PHP 7.2 or above.

Installation

a) with Composer:

composer require adamale/nbpfetch

b) without Composer:
include Composer File Loader in your project to autoload all the necessary classes.

Usage

Currency rate

<?php

use NBPFetch\NBPFetch;

require_once "vendor/autoload.php";

$NBPFetch = new NBPFetch();
$NBPFetchCurrencyRate = $NBPFetch->currencyRate();

/**
 * Currency rate examples.
 * Available methods are: current(string $currency), today(string $currency)
 * byDate(string $currency, string $date), byDateRange(string $currency, string $from, string $to)
 * and last(string $currency, int count).
 */
try {
    $currentCurrencyRate = $NBPFetchCurrencyRate->current("EUR");
    $todayCurrencyRate = $NBPFetchCurrencyRate->current("EUR");
    $givenDateCurrencyRate = $NBPFetchCurrencyRate->byDate("EUR", "2019-08-28");
    $givenDateRangeCurrencyRates = $NBPFetchCurrencyRate->byDateRange("EUR", "2019-08-01", "2019-08-31");
    $last10CurrencyRates = $NBPFetchCurrencyRate->last("EUR", 10);
} catch (Exception $e) {
}

Exchange rate table

<?php

use NBPFetch\NBPFetch;

require_once "vendor/autoload.php";

$NBPFetch = new NBPFetch();
$NBPFetchExchangeRateTable = $NBPFetch->exchangeRateTable();

/**
 * Exchange rate table examples.
 * Available methods are: current(string $table), today(string $table),
 * byDate(string $table, string $date), byDateRange(string $table, string $from, string $to)
 * and last(string $table, int count)
 */
try {
    $currentExchangeRateTable = $NBPFetchExchangeRateTable->current("A");
    $todayExchangeRateTable = $NBPFetchExchangeRateTable->today("A");
    $givenDateExchangeRateTable = $NBPFetchExchangeRateTable->byDate("A", "2019-08-28");
    $givenDateRangeExchangeRateTables = $NBPFetchExchangeRateTable->byDateRange("A", "2019-08-01", "2019-08-31");
    $last10ExchangeRateTables = $NBPFetchExchangeRateTable->last("A", 10);
} catch (Exception $e) {
}

Gold price

<?php

use NBPFetch\NBPFetch;

require_once "vendor/autoload.php";

$NBPFetch = new NBPFetch();
$NBPFetchGoldPrice = $NBPFetch->goldPrice();

/**
 * Gold price examples.
 * Available methods are: current(), today(), byDate(string $date),
 * byDateRange(string $from, string $to) and last(int count)
 */
try {
    $currentGoldPrice = $NBPFetchGoldPrice->current();
    $todayGoldPrice = $NBPFetchGoldPrice->today();
    $givenDateGoldPrice = $NBPFetchGoldPrice->byDate("2019-08-28");
    $givenDateRangeGoldPrices = $NBPFetchGoldPrice->byDateRange("2019-08-01", "2019-08-31");
    $last10GoldPrices = $NBPFetchGoldPrice->last(10);
} catch (Exception $e) {
}

About

Author

Adam Aleksak kontakt@adamaleksak.pl

License

NBPFetch is licensed under the MIT License - see the LICENSE file for the details.