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;
}
2
Answers
Quick heads up, since the result looks to be in JSON format, you first may want to call json_decode on the result to get it in PHP Object format.
Your API example after json_decode would look like this:
Next step is to use your function to combine both parameters to access the (for example) USDAED result:
As mentioned by Gary Thomas already, the CurrencyLayer API documentation has a source currency switching parameter that allows you to switch the base currency from
USD
to whatever your$currency1
parameter is set to.As I understand however, you want to be able to periodically query the CurrencyLayer API with only
USD
as the source currency, and perform the rate calculation yourself.To achieve this, you need to convert:
CURRENCY 1
toUSD
USD
toCURRENCY 2
Which translates into code as:
You can also use a money library such as brick/money, that handles these calculations (and much more) for you:
The
BaseCurrencyProvider
is designed for this very purpose, when you have a list of rates relative to a single currency, and want to convert between two arbitrary currencies in the list.Note that in a real-world app, you would probably use a PDOProvider to load exchange rates directly from your database, instead of the
ConfigurableProvider
used above.