PHP code example of rapidwebltd / simple-currency-rates

1. Go to this page and download the library: Download rapidwebltd/simple-currency-rates library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rapidwebltd / simple-currency-rates example snippets


use RapidWeb\SimpleCurrencyRates\SimpleCurrencyRates;

$rates = (new SimpleCurrencyRates())->get('GBP');

[
    'EUR' => 1.16,
    'GBP' => 1.0,
    'USD' => 1.35,
    'XBT' => 0.00001234,
]

$rates = new SimpleCurrencyRates($psr16Cache);

// Or replace it later.
$rates->setCache($psr6CachePool);

use RapidWeb\SimpleCurrencyRates\Cache\FilesystemCache;

$rates = new SimpleCurrencyRates(
    new FilesystemCache('/var/cache/my-application/currency-rates')
);

use RapidWeb\SimpleCurrencyRates\Contracts\RateSourceInterface;

class CompanyRateSource implements RateSourceInterface
{
    public function getRates($base)
    {
        return [
            'EUR' => 1.16,
            'USD' => 1.35,
        ];
    }

    public function getCacheTtl()
    {
        return 300;
    }
}

$rates = new SimpleCurrencyRates($cache, [
    new CompanyRateSource(),
]);

$rates
    ->setSources([new CompanyRateSource()])
    ->addSource($anotherSource);

use RapidWeb\SimpleCurrencyRates\Sources\FrankfurterRateSource;

$source = new FrankfurterRateSource(function ($url) use ($httpClient) {
    return $httpClient->get($url)->getBody()->getContents();
});