PHP code example of kulizh / currency

1. Go to this page and download the library: Download kulizh/currency 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/ */

    

kulizh / currency example snippets



Currency\Converter;

$converter = new Converter();

$converter->from('RUB')->to('USD');

$thaiMarket = $covnerter->marketFactory('BankOfThai');


namespace MyMarket;

use Currency\Helpers\MarketCache;
use Currency\Market\IMarket;

class CentralBankOfRussia implements IMarket
{
    /* 
    * Here is the string we parse.
    * This could be API Url or whatever
    **/
    protected string $url = 'https://cb.ru/rates.json';

    /**
     * $from string isoCode of From currency
     * $to string isoCode of To currency
     * 
     * @return float Currency result
     */
    public function getRate(string $from, string $to): float
    {
        // Optional: you can cache data to avoid ban or freeze
        $data = MarketCache::read($this->url, 'cb.rf');

        $data_decoded = json_decode($data, true);

        /*
        * Place your script to get rates FROM or TO here
        **/

        return 0.00;
    }
}


$myMarket = new MyMarket\CentralBankOfRussia();

$converter->useMarket($myMarket);

use Currency\Converter;

$usdPrice = 13670;

$converter = new Converter();
$converter->from('usd')->to('rub');

$rubPrice = $converter->convert($usdPrice);