PHP code example of ordinary9843 / currency-master

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

    

ordinary9843 / currency-master example snippets



Ordinary9843\CurrentMaster;
use Ordinary9843\Constants\ExchangeRateTypeConstant;
use Ordinary9843\Constants\SourceConstant;

$currentMaster = new CurrentMaster();

/**
 * Sets the data source to be used.
 */
$currentMaster->setSourceType(SourceConstant::BOT);

/**
 * Sets the duration, in seconds, for which the data source should be cached.
 */
$currentMaster->setCacheTimeInSeconds(60);

/**
 * Retrieves all exchange rates from the source set. 
 *
 * Output:
 * [
 *   'TWD' => [
 *     'currency' => 'TWD',
 *     'cashBuy' => 1,
 *     'cashSell' => 1,
 *     'spotBuy' => 1,
 *     'spotSell' => 1
 *   ],
 *   'USD' => [
 *     'currency' => 'USD',
 *     'cashBuy' => 31.465,
 *     'cashSell' => 32.135,
 *     'spotBuy' => 31.815,
 *     'spotSell' => 31.915
 *   ]
 * ]
 */
$currentMaster->getExchangeRates();

/**
 * Converts a specified amount of money from one currency to another.
 *
 * Output: [
 *  'fromCurrency' => 'TWD',
 *  'toCurrency' => 'USD',
 *  'convertExchangeRate' => '0.031690698779',
 *  'convertedAmount' => '31.690698779000',
 *  'formattedConvertedAmount' => '$31.690698779000',
 *  'currencySymbol' => '$'
 * ]
 */
$currentMaster->exchangeRate(CurrencyConstant::TWD, CurrencyConstant::USD, ExchangeRateTypeConstant::CASH_BUY, 1000);