rogerthomas84/ohexchange

OhExchange is an exchange rate library for PHP, utilising the European Central Bank XML feed.

1.0.0 2019-06-15 07:58 UTC

This package is auto-updated.

Last update: 2024-10-15 20:08:12 UTC


README

OhExchange is a simple library to retrieve exchange rates from the European Central Bank.

Retrieving the data

<?php
require 'vendor/autoload.php';

use OhExchange\OhExchangeException;
use OhExchange\OhExchangeService;

try {
    $models = OhExchangeService::getLatestRates();
    foreach ($models as $model) {
        echo $model->date->format('Y-m-d') . PHP_EOL;
        foreach ($model->rates as $currency => $exchange) {
            echo '  ' . $currency . ' -> ' . $exchange . PHP_EOL;
        }
        // Persist this model, using the date as a unique identifier.
    }
} catch (OhExchangeException $e) {
    echo 'Error:' . PHP_EOL;
    echo '  ' . $e;
}

Converting between currencies:

<?php
$model = new \OhExchange\OhExchangeDto(); // Should be retrieved from your database.

$valueInUsd = $model->convertAmountFromTo(1, 'GBP', 'USD');