# 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](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

```bash
composer require shipsaas/currency-fx

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

### Supporting Services / Batteries

* [https://exchangeratesapi.io/](https://exchangeratesapi.io/)
    
* [https://exchangerate.host/](https://exchangerate.host/)
    
* [https://fixer.io/](https://fixer.io/)
    
* [https://currencylayer.com/](https://currencylayer.com/)
    
* [https://www.currencycloud.com/](https://www.currencycloud.com/)
    

### Usage

Simply initialize the class with the required parameters. And it is ready to use in no time.

```php
$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)

```php
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!
