rubin/openexchangerates

There is no license information available for the latest version (1.0.2) of this package.

OpenExchangeRates API connector

1.0.2 2024-10-05 20:55 UTC

This package is auto-updated.

Last update: 2024-12-05 21:16:31 UTC


README

Build Status Latest Stable Version Coverage Status PHP Version Require GitHub License

PHP implementation for the OpenExchangeRates.org REST API. This library is based on the REST API docs.

Installation

With composer:

composer require rubin/openexchangerates

Usage

Create API connector:

$api = new OpenExchangeRates\OpenExchangeRatesApi('{APP_ID}');

Extra parameters

Set Base Currency ('base')

The default base currency of the API is US Dollars (USD), but you can request exchange rates relative to a different base currency, where available, by setting the base parameter in your request.

$api->setBase('EUR');

Get Specific Currencies ('symbols')

By default, the API returns rates for all currencies, but if you need to minimise transfer size, you can request a limited subset of exchange rates, where available, by setting the symbols parameter in your request.

$api->setSymbols(['AMD', 'EUR']);

Alternative Rates ('show_alternative')

You may now request latest and historical rates for unofficial, black market and alternative digital currencies by adding a simple API parameter onto your request.

$api->setShowAlternative(true);

API endpoints

/latest

Get the latest exchange rates available from the Open Exchange Rates API.

The most simple route in our API, latest.json provides a standard response object containing all the conversion rates for all of the currently available symbols/currencies, labeled by their international-standard 3-letter ISO currency codes.

The latest rates will always be the most up-to-date data available on your plan.

echo "Symbol\tRate\n";
foreach ($api->getLatest()->rates as $symbol => $rate) {
    printf("%s\t%s\n", $symbol, $rate);
}

/historical

Get historical exchange rates for any date available from the Open Exchange Rates API, currently going back to 1st January 1999.

The historical rates returned are the last values published for a given UTC day (up to and including 23:59:59 UTC), except for the current UTC date.

echo "Symbol\tRate\n";
foreach ($api->getHistorical(new \DateTimeImmutable('2012-07-10'))->rates as $symbol => $rate) {
    printf("%s\t%s\n", $symbol, $rate);
}

/currencies

Get a JSON list of all currency symbols available from the Open Exchange Rates API, along with their full names, for use in your integration.

This list will always mirror the currencies available in the latest rates (given as their 3-letter codes).

echo "Symbol\tName\n";
foreach ($api->getCurrencies() as $symbol => $name) {
    printf("%s\t%s\n", $symbol, $name);
}

/usage

Get basic plan information and usage statistics for an Open Exchange Rates App ID

var_dump($api->getUsage());