DEV Community

Seth Phat
Seth Phat

Posted on

PHP CurrencyFX Library

Hi all,

I'm happy to share the latest library I've been working on.

It is called CurrencyFX.

Let's check it out 😆

Introduction

CurrencyFX is a PHP Library that helps you retrieve FX Rates from multiple online services, batteries-included 🔋🔋🔋

Repository: https://github.com/shipsaas/currency-fx

Requirement: PHP 8.1+

  • Additionally, Laravel 10 (for Laravel auto integration)

100% tested and covered under Unit, Integration & E2E tests (assert against the real services)

Installation

composer require shipsaas/currency-fx

# (Optional) For Laravel users
php artisan vendor:publish --tag=currency-fx-configs
Enter fullscreen mode Exit fullscreen mode

Supporting Services / Batteries

Usage

Simply initialize the class with the required parameters (which are the API Key, Credentials). And it is ready to use in no time.

$service = new CurrencyCloudService($host, $loginId, $apiKey);
$rateResponse = $service->getRates('USD', 'SGD');

if (!$rateResponse->isOk()) {
    // failed to get the rate from third party service
    // do something here
}

$rate = $rateResponse->getOkResult()->rate; // float (1.4xxx)
Enter fullscreen mode Exit fullscreen mode

Laravel Usage

(Please add the needful ENVs before using, check out the currency-fx.php for more info)

use CurrencyFX\Services\CurrencyLayerService;
use CurrencyFX\Services\ExchangerRatesApiIoService;

// use the global "app"
app(CurrencyLayerService::class)->getRates('USD', 'EUR');

// DI
class TransferService
{
    public function __construct(
        private ExchangerRatesApiIoService $rateService
    ) {
    }

    public function transfer(): TransferResult
    {
        $rateRes = $this->rateService->getRates('EUR', 'GBP');
        if ($rateRes->isError()) {
            return TransferResult::error(...);
        }

        $rate = $rateRes->getOkResult()->rate;
    }
}
Enter fullscreen mode Exit fullscreen mode

Final Words

Thank you, please give it a ⭐️⭐️⭐️ to support the project.

Don't forget to share with your friends & colleagues, so they can also build their own SaaS products as well 🚀

Cheers!

Top comments (0)