<?php 

include('../classes/database.class.php');
$db = new database();
$audlist = "AUD_USD%2CAUD_NZD%2CAUD_CAD%2CAUD_CHF%2CAUD_JPY%2CAUD_HKD%2CAUD_SGD%2CCAD_CHF%2CCAD_JPY%2CCAD_HKD%2CCAD_SGD%2CCHF_JPY%2CCHF_HKD%2CEUR_USD%2CEUR_AUD%2CEUR_GBP%2CEUR_NZD%2CEUR_CAD%2CEUR_CHF%2CEUR_JPY%2CEUR_HKD%2CEUR_SGD%2CGBP_USD%2CGBP_AUD%2CGBP_NZD%2CGBP_CAD%2CGBP_CHF%2CGBP_JPY%2CGBP_HKD%2CGBP_SGD%2CHKD_JPY%2CNZD_USD%2CNZD_CAD%2CNZD_CHF%2CNZD_JPY%2CNZD_HKD%2CNZD_SGD%2CSGD_CHF%2CSGD_JPY%2CSGD_HKD%2CUSD_JPY%2CUSD_CHF%2CUSD_SGD%2CUSD_HKD%2CUSD_CAD";
$resultsAud = get_heatmap_rate($audlist, $db);
print_r(json_encode($resultsAud));

function get_heatmap_rate($currencyList, $db)
{

	$ch = curl_init("https://api-fxtrade.oanda.com/v3/accounts/001-001-414778-001/pricing?instruments=" . $currencyList);
	$header = array();
	$header[] = 'Content-type: application/json';
	$header[] = 'Authorization: Bearer c143ece96a9cb2ce9dc6ecdbaf844523-77cf55b79018d06cf3396c90855d06be';
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	$resultsj = curl_exec($ch);
	$results = json_decode($resultsj);
	$returnArray = array();

foreach($results->prices as $result){
			$instrument = $result->instrument;
			$bidPrice = $result->bids[0]->price;
			$time = date('H:i');
			$date = date('Y-m-d');
			$lastDay = date('Y-m-d', strtotime('-1 day', strtotime($date)));
			$getOldRate = $db->db->prepare("SELECT * FROM heatmap WHERE date = '$date' AND symbol = '$instrument'");
			$getOldRate->execute();
			$fetchRate = $getOldRate->fetch();
				if (empty($fetchRate)) {
					if ($time > "16:00") {

						$stmt = $db->db->prepare("INSERT INTO heatmap(symbol, price, date) VALUES ('$instrument', '$bidPrice', '$date')");
						$stmt->execute();
					}
			}
			$getLastRate = $db->db->prepare("SELECT * FROM heatmap WHERE date = '$lastDay' AND symbol = '$instrument'");
			$getLastRate->execute();
			$lastRate = $getLastRate->fetch();
			$lastPrice = $lastRate['price'];
			$Difference = $bidPrice - $lastPrice;
			$ResultDifference = $Difference / $bidPrice * 100;
			$per = number_format((float)$ResultDifference, 2, '.', '');
			$array = array('sym'=>$instrument, 'bid'=>$bidPrice, 'per'=>$per);
			$array = json_encode($array);
			array_push($returnArray, $array);



}


return $returnArray;

}

?>