Skip to main content

PHP CurrencyFX Library

Published 2 min readMarkdown
Part of seriesTech tips
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 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!

About the author

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

No comments yet. Be the first to comment.

More from this blog