db->query($sql, [$this->idApi]); if (!$qry) { $err = $this->db->error(); if ($err["code"] == "1146") { $sql = "create table ext_api_key( id varchar(20) primary key, apiKey varchar(100) )"; $qry = $this->db->query($sql); } if (!$qry) { echo json_encode(["status" => "ERR", "message" => "API Init Error SyncKey"]); exit; } $sql = "insert into ext_api_key values(?,?)"; $this->db->query($sql, [$this->idApi, $this->defautApi]); return; } $rows = $qry->result_array(); if (count($rows) == 0) { echo json_encode(["status" => "ERR", "message" => "API Key not Exist"]); exit; } $this->defautApi = $rows[0]["apiKey"]; } public function __construct() { parent::__construct(); $this->get_api_key(); $this->check_api(); } function check_api() { // Check if the Authorization header is set if (isset($_SERVER['HTTP_AUTHORIZATION'])) { $authHeader = $_SERVER['HTTP_AUTHORIZATION']; // Extract the token from the header if (preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) { $accessToken = $matches[1]; if ($accessToken == $this->defautApi) { return; } } else { //Not authorized http_response_code(401); exit; } } else { http_response_code(401); } } public function instrument() { $sql ="select Nat_InstrumentID, Nat_InstrumentName from nat_instrument join t_instrument_local on Nat_InstrumentID = T_InstrumentLocalNat_InstrumentID and Nat_InstrumentIsActive ='Y' and T_InstrumentLocalIsActive ='Y'"; $qry = $this->db->query($sql); if(!$qry){ echo json_encode(["status"=>"ERR","message"=> "Err get nat_instrument"]); exit; } $rows = $qry->result_array(); echo json_encode(["status"=>"OK","data"=>$rows]); } public function raw_data() { $prm = $this->sys_input; if (!isset($prm["date"])){ echo json_encode(["status"=>"ERR", "message"=>"Missing date parameter"]); exit; } if (!isset($prm["instrumentID"])){ echo json_encode(["status"=>"ERR", "message"=>"Missing instrumentID parameter"]); exit; } $date = $prm["date"]; $instrumentID = $prm["instrumentID"]; $page = 1; if (isset($prm["page"])){ $page = $prm["page"]; } $sql = "select count(*) total from itf_raw where itf_RawDate >=? and itf_RawDate <= ? and itf_RawNat_InstrumentID =?"; $sd = $date . " 00:00:01"; $ed = $date . " 23:59:59"; $qry = $this->db->query($sql,[$sd,$ed,$instrumentID]); if (!$qry){ echo json_encode(["status"=>"ERR","message"=> "Err get total raw data"]); exit; } $rows = $qry->result_array(); if (count($rows)==0) { echo json_encode(["status"=>"ERR","message"=> "Err get total raw data rows"]); exit; } $total = ceil($rows[0]["total"] / $this->maxPerRow); $offset = ($page - 1) * $this->maxPerRow; $sql = "select itf_RawData data from itf_raw where itf_RawDate >= ? and itf_RawDate <= ? and itf_RawNat_InstrumentID = ? limit ?,?"; $qry = $this->db->query($sql,[$sd,$ed,$instrumentID, $offset,$this->maxPerRow]); if (!$qry){ echo json_encode(["status"=>"ERR","message"=> "Err get raw data"]); exit; } $rows = $qry->result_array(); echo json_encode(["status" => "OK", "maxPage"=>$total, "data" =>$rows]); } }