I am currently developing an application using PHP that users could change the money currency of the products, like ebay or aliexpress do. So, if the user change his currency to USD all products prices are going to be converted to USD.
I have searched for an API to get real time currencies called CurrencyLayer. The API offers the following structure:
"success": true,
"terms": "https://currencylayer.com/terms",
"privacy": "https://currencylayer.com/privacy",
"timestamp": 1432480209,
"source": "USD",
"quotes": {
"USDAED": 3.67315,
"USDAFN": 60.790001,
"USDALL": 126.194504,
"USDAMD": 477.359985,
"USDANG": 1.790403,
[...]
}
My plan is to save this quotes every hour in my database. Considering a function that converts currencies, what would be the correct algorithm to convert one to other? I know it is not difficult but I could not figure it out.
function convertCurrency($currency1 = 'USD', $currency2 = 'EUR', $value){
//Search the currency value and algorithm to convert
$newValue = (????)
return $newValue;
}