PHP CurrencyFX Library

Photo by PiggyBank on Unsplash

PHP CurrencyFX Library

ยท

2 min read

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 tests & Integration tests (assert against the real services)

Install

composer require shipsaas/currency-fx

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

Supporting Services / Batteries

Usage

Simply initialize the class with the required parameters. 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)

Laravel Usage

(Please remember to add the needful ENVs before using)

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

// global access
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;
    }
}

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!

ย