Skip to main content

Command Palette

Search for a command to run...

PHP CurrencyFX Library

Updated
β€’2 min read
PHP CurrencyFX Library
S

develops awesome software, contributes to OSS, writes tech tips, and loves Vietnamese milk coffee!

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!

More from this blog

S

Seth Phat Tech Blog

69 posts

develops awesome software, contributes to OSS, writes tech tips, and loves Vietnamese milk coffee!