You usually get Exchange Rate data from other website through Bank or third party API call, so what you can do to get the same thing without API. As you know, exchange rate is very important for all online POS system and other sale web applications. It must be easy if you are given the ready-to-use API from any exchange rate data provider, but making your own data extraction from the exchange rate website is very hard and not reliable. However, here you will be given the best reliable choice to get exchange rate data from any bank or third party websites. Below is the custom PHP script allowing you to get exchange rate data from Cambodia ACLEDA Bank for Khmer Riel currency.
<html> <head> <meta charset="utf-8"> <title>Realtime ACLEDA Bank Exchange Rate without API | learnweb.top</title> </head> <body style="text-align: center; padding-top: 50px;"> <h2>Realtime ACLEDA Bank Exchange Rate without API</h2> <form action="" method="post"> Currency: <select name="currency"> <?=$currency_option?> </select> <button type="submit">Go</button> </form> <div style="padding-top: 10px; width: 300px; margin: auto;"><?=$rate?></div> </body> </html>
This html is just for demo. You can make your own output design.
//set time zone date_default_timezone_set('Asia/Phnom_Penh');$datetime = date("d/m/Y H:i:s"); //url source $url= 'https://www.acledabank.com.kh/assets/unity/exchangerate'; //check if specific currency is set, default USD. $currency='USD';$rate='N/A';$currency_option = '';$tblarr=array(); if(isset($_POST['currency']) and $_POST['currency']<>''){$currency=$_POST['currency'];} //create html element data from url $doc = new DOMDocument(); $doc->loadHTMLFile($url); $tags = $doc->getElementsByTagName('tr'); //loop to copy table data to array foreach ($tags as $tag) { $curr=$tag->childNodes[0]->nodeValue; $bid=$tag->childNodes[1]->nodeValue; $ask=$tag->childNodes[2]->nodeValue; //select only KHR rate if(strpos($bid, "KHR") !== false){ $tblarr[$curr]=array('currency'=>$curr,'bid'=>str_replace('KHR ','',$bid),'ask'=>str_replace('KHR ','',$ask)); $currency_option .= '<option value="'.$curr.'" '.($curr==$currency?'selected':'').'>'.$curr.'</option>'; } } //prepare html output if(isset($tblarr[$currency])){ $selectedrate = $tblarr[$currency]; $rate='<div>'.$selectedrate['currency'].' to Khmer Riel (KHR)<br /><code>Date: '.$datetime.'</code></div><hr /><div>Bid = '.$selectedrate['bid'].' រៀល</div><div>Ask = '.$selectedrate['ask'].' រៀល</div>'; }
All the important jobs are from this php script. First, you need to have the exchange rate website url as the data source to extract the required data. Then, you need to parse the string getting from that URL to PHP html element data before starting the extracting. Once the PHP html element data is ready to use, you need to find the data table in that html and its data value to push it to an array data. Finally, you have complete data in an array format, you can call it to use on your demand.
array (size=9) 'USD' => array (size=3) 'currency' => string 'USD' (length=3) 'bid' => string '4,094' (length=5) 'ask' => string '4,109' (length=5) 'THB' => array (size=3) 'currency' => string 'THB' (length=3) 'bid' => string '122.60' (length=6) 'ask' => string '124.10' (length=6) 'EUR' => array (size=3) 'currency' => string 'EUR' (length=3) 'bid' => string '4,292.56' (length=8) 'ask' => string '4,747.95' (length=8) 'AUD' => array (size=3) 'currency' => string 'AUD' (length=3) 'bid' => string '2,594.37' (length=8) 'ask' => string '3,043.54' (length=8) 'VND' => array (size=3) 'currency' => string 'VND' (length=3) 'bid' => string '0.1671' (length=6) 'ask' => string '0.1834' (length=6) 'CAD' => array (size=3) 'currency' => string 'CAD' (length=3) 'bid' => string '2,869.48' (length=8) 'ask' => string '3,311.44' (length=8) 'LAK' => array (size=3) 'currency' => string 'LAK' (length=3) 'bid' => string '0.2336' (length=6) 'ask' => string '0.3942' (length=6) 'JPY' => array (size=3) 'currency' => string 'JPY' (length=3) 'bid' => string '33.0241' (length=7) 'ask' => string '36.3082' (length=7) 'GBP' => array (size=3) 'currency' => string 'GBP' (length=3) 'bid' => string '5,002.05' (length=8) 'ask' => string '5,542.22' (length=8)
This is the complete array output you have from the source URL. You can do things like this for other exchange rate websites by just modifying the table data extraction process if those websites have different html table design.
For the real-time feature, you can use this php script with Ajax call or make a cron job in your cPanel to make the latest data update from the source url. That is it! Hope it is helpful!