Files
2026-04-15 15:23:57 +07:00

213 lines
6.4 KiB
PHP

<?php
class Ssapi extends MY_Controller {
function __construct() {
parent::__construct();
$this->db_onedev = $this->load->database("default", true);
}
function test_auth() {
//$this->load->library("Satusehat");
$result = $this->put_token();
echo $result;
}
function test_gettoken() {
//$this->load->library("Satusehat");
$result = $this->get_token();
echo $result;
}
function test_search_practicioner_by_nik(){
$nik = "367400001111202";
$result = $this->search_practicioner_by_nik($nik);
$birthDate = $result->entry[0]->resource->birthDate;
$gender = $result->entry[0]->resource->gender;
$ihsNumber = $result->entry[0]->resource->id;
$dataPracticioner = array(
'birthDate' => $birthDate,
'gender' => $gender,
'ihsNumber' => $ihsNumber
);
print_r($dataPracticioner);
}
function test_search_patient_by_nik(){
$nik = "9271060312000001";
$result = $this->search_patient_by_nik($nik);
$ihsNumber = $result->entry[0]->resource->id;
$name = $result->entry[0]->resource->name[0]->text;
$dataPatient = array(
'name' => $name,
'ihsNumber' => $ihsNumber
);
print_r($dataPatient);
}
function put_token(){
$auth_url = "https://api-satusehat-dev.dto.kemkes.go.id/oauth2/v1";
//API URL
$url = $auth_url."/accesstoken?grant_type=client_credentials";
//echo $url;
$data = [
"client_id" => "6PukKqO0RQqu0cKBOC8EKGcXQySfPR4aVkiVmuTgkx5xvva4",
"client_secret" => "89ZqsmY3z5W7rVscHTp9gJoAWWiAZG4A2unS3maTw3DxBFxTdaRsSeTUbD8mRN3p"
];
$ch = curl_init($url);
# Setup request to send json via POST.
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'Content-Type: application/x-www-form-urlencoded'
)
);
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
//print_r($result);
//echo $token_rst->access_token;
if($result){
$token_rst = json_decode($result);
$sql = "SELECT COUNT(*) as xcount, tokenID
FROM one_health.token
WHERE
tokenIsActive = 'Y'
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "get count token error";
exit;
}
$rst_count = $qry->row_array();
if($rst_count['xcount'] > 0){
$sql = "UPDATE one_health.token SET tokenValue = ?, tokenExpired = DATE_ADD(NOW(), INTERVAL 50 MINUTE)
WHERE tokenID = ?";
$qry = $this->db_onedev->query($sql, [$token_rst->access_token,$rst_count['tokenID']]);
if (!$qry) {
$this->sys_error_db("refresh token error", $this->db_onedev->last_query());
exit;
}
}else{
$sql = "UPDATE one_health.token SET tokenIsActive = 'N' WHERE tokenIsActive = 'Y'";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "nonactive token error";
exit;
}
$sql = "INSERT INTO one_health.token(tokenValue,tokenExpired) VALUES(?,DATE_ADD(NOW(), INTERVAL 50 MINUTE))";
$qry = $this->db_onedev->query($sql, [$token_rst->access_token]);
if (!$qry) {
echo "insert token error";
exit;
}
}
$sql = "SELECT tokenValue
FROM one_health.token
WHERE
tokenIsActive = 'Y' LIMIT 1
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "get token error";
exit;
}
return $qry->row()->tokenValue;
}
}
function get_token(){
$sql = "SELECT COUNT(*) as xcount, tokenValue
FROM one_health.token
WHERE tokenIsActive = 'Y' AND NOW() < tokenExpired AND tokenValue IS NOT NULL
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "select token error";
exit;
}
$data_token = $qry->row_array();
//print_r($data_token);
if($data_token['xcount'] > 0){
return $data_token['tokenValue'];
}else{
return $this->put_token();
}
}
function search_practicioner_by_nik($nik){
$token = $this->get_token();
$authorization = "Authorization: Bearer ".$token;
$xbase_url = "https://api-satusehat-dev.dto.kemkes.go.id/fhir-r4/v1";
//API URL
$url = $xbase_url."/Practitioner?identifier=https://fhir.kemkes.go.id/id/nik|".$nik;
//echo $url;
$ch = curl_init($url);
# Setup request to send json via POST.
//$payload = json_encode($data);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
$data_rst = json_decode($result);
//print_r($result);
return $data_rst;
}
function search_patient_by_nik($nik){
$token = $this->get_token();
$authorization = "Authorization: Bearer ".$token;
$xbase_url = "https://api-satusehat-dev.dto.kemkes.go.id/fhir-r4/v1";
//API URL
$url = $xbase_url."/Patient?identifier=https://fhir.kemkes.go.id/id/nik|".$nik;
//echo $url;
$ch = curl_init($url);
# Setup request to send json via POST.
//$payload = json_encode($data);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
$data_rst = json_decode($result);
//print_r($result);
return $data_rst;
}
}