The bank of Canada provides RSS feed for these currencies :
AUD, BRL, CNY, EUR, HKD, INR, IDR, JPY, MXN, NZD, NOK, PEN, RUB, SAR, SGD, ZAR, KRW, SEK, CHF, TWD, TRY, GBP, USD
Here is a way of getting currency conversions without API or 3rd party service:
<?php
class EXCHANGE {
public $Rates;
public $Rate;
public function __construct(){
$this->Rates = $this->fetchAllRates();
foreach($this->Rates as $currency => $rate){
$this->Rate[$currency] = $rate['latest'];
}
}
public function fetchAllRates(){
$currencies = ["AUD","BRL","CNY","EUR","HKD","INR","IDR","JPY","MXN","NZD","NOK","PEN","RUB","SAR","SGD","ZAR","KRW","SEK","CHF","TWD","TRY","GBP","USD"];
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, "https://www.bankofcanada.ca/valet/observations/group/FX_RATES_DAILY/json?start_date=2010-01-01");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
$rates = curl_exec($cURL);
curl_close($cURL);
$rates = json_decode($rates,true)['observations'];
foreach($currencies as $currency){
foreach($rates as $rate){
$AllRates[$currency][$rate['d']] = $rate['FX'.$currency.'CAD']['v'];
$AllRates[$currency]['latest'] = $rate['FX'.$currency.'CAD']['v'];
}
}
return $AllRates;
}
public function convert($value,$from,$to){
if($to == "CAD"){ return $value*$this->Rate[$from]; }
else { return ($value*$this->Rate[$from])/$this->Rate[$to]; }
}
}
$Exchange = new EXCHANGE();
foreach($Exchange->Rate as $currency => $rate){
echo $currency.': '.$rate."<br>\n"; // Listing all the exchange rates
}
echo "Converting 2.00 USD to CAD : ".$Exchange->convert(2,"USD","CAD")."\n"; //2022-02-23 = 2.5486
echo "Converting 2.00 USD to AUD : ".$Exchange->convert(2,"USD","AUD")."\n"; //2022-02-23 = 2.7708197434225
Update:
I had initially forgot to put the convert method.
Information:
This class is using the RSS feed of the Bank of Canada. It is not the most accurate data, because it is only updated once per business day. The $Rate property contains the exchange rates for CAD currency. Thus to convert other currencies, it first converts the initial currency to CAD and then to the new currency. So in the example provided above, 2.00 USD is converted to 2.5486 CAD. Then divided by the exchange rate of AUD resulting in 2.7708197434225 AUD.