288 lines
8.3 KiB
PHP
288 lines
8.3 KiB
PHP
<?php
|
|
class Ssapix 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;
|
|
}
|
|
|
|
function send_bundle($orderID){
|
|
|
|
$sql = "SELECT *
|
|
FROM one_health.bundle
|
|
WHERE BundleT_orderHeaderID = {$orderID} AND BundleIsActive = 'Y'
|
|
LIMIT 1
|
|
";
|
|
$qry = $this->db_onedev->query($sql);
|
|
if (!$qry) {
|
|
echo "select bundle error";
|
|
exit;
|
|
}
|
|
|
|
$data_blundle = $qry->row_array();
|
|
// print_r($data_blundle);
|
|
$json_data = json_decode($data_blundle['BundleJSON']);
|
|
$json_payload = json_encode($json_data);
|
|
|
|
$token = $this->get_token();
|
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt_array($curl, array(
|
|
CURLOPT_URL => 'https://api-satusehat-dev.dto.kemkes.go.id/fhir-r4/v1',
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => '',
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 0,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => 'POST',
|
|
CURLOPT_POSTFIELDS => $json_payload,
|
|
CURLOPT_HTTPHEADER => array(
|
|
'Content-Type: application/json',
|
|
'Authorization: Bearer '.$token
|
|
),
|
|
));
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
//echo $response;
|
|
|
|
//$json_response = json_encode(json_decode($response));
|
|
//echo $json_response;
|
|
$sql = "UPDATE one_health.bundle SET BundleResponseJSON = '{$response}'
|
|
WHERE BundleT_orderHeaderID = {$orderID} AND BundleIsActive = 'Y'
|
|
";
|
|
$qry = $this->db_onedev->query($sql);
|
|
//echo $sql;
|
|
if (!$qry) {
|
|
echo "update bundle error";
|
|
exit;
|
|
}
|
|
|
|
echo $response;
|
|
}
|
|
|
|
protected function objToArray($obj)
|
|
{
|
|
// Not an object or array
|
|
if (!is_object($obj) && !is_array($obj)) {
|
|
return $obj;
|
|
}
|
|
|
|
// Parse array
|
|
foreach ($obj as $key => $value) {
|
|
$arr[$key] = $this->objToArray($value);
|
|
}
|
|
|
|
// Return parsed array
|
|
return $arr;
|
|
}
|
|
|
|
|
|
} |