Contoh bridging BPJS dengan PHP (Update SEP)

<?php

$dataid = "1234"; //Ganti dengan consumerID dari BPJS
$secretKey = "5678"; //Ganti dengan consumerSecret dari BPJS
$localIP = "dvlp.bpjs-kesehatan.go.id";
$url = "http://".$localIP."/devwslokalrest/SEP/update"; //Lihat katalog, jangan sertakan port
$port = 8081; //port url

date_default_timezone_set('UTC');
date_default_timezone_set('Asia/Jakarta');

$tStamp = strval(time() - strtotime('1970-01-01 00:00:00'));
$signature = hash_hmac('sha256', $dataid . "&" . $tStamp, $secretKey, true);
$encodedSignature = base64_encode($signature);
$urlencodedSignature = urlencode($encodedSignature);

echo "X-cons-id: " . $dataid . "<br>";
echo "X-timestamp:" . $tStamp . "<br>";
echo "X-signature: " . $encodedSignature . "<br>";

function post_request($url, $port, $dataid, $tStamp, $encodedSignature, $data, $referer = '')
{
 
 //-Convert the data array into URL Parameters like a=b&foo=bar etc.
 //$data = http_build_query($data);
 
 // parse the given URL
 $url = parse_url($url);
 
 if ($url['scheme'] != 'http') {
 die('Error: Only HTTP request are supported !');
 }
 
 // extract host and path:
 $host = $url['host'];
 $path = $url['path'];
 
 // open a socket connection on port 80 - timeout: 50 sec
 $fp = fsockopen($host, $port, $errno, $errstr, 50);
 
 if ($fp) {
 
 // send the request headers:
 fputs($fp, "PUT $path HTTP/1.1\r\n");
 fputs($fp, "Host: $host\r\n");
 
 if ($referer != '')
 fputs($fp, "Referer: $referer\r\n");
 
 fputs($fp, "x-cons-id: " . $dataid . "\r\n");
 fputs($fp, "x-timestamp: " . $tStamp . "\r\n");
 fputs($fp, "x-signature: " . $encodedSignature . "\r\n");
 fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
 fputs($fp, "Content-length: " . strlen($data) . "\r\n");
 fputs($fp, "Connection: close\r\n\r\n");
 fputs($fp, $data);
 
 $result = '';
 while (!feof($fp)) {
 // receive the results of the request, 128 char
 $result .= fgets($fp, 128);
 }
 } else {
 return array(
 'status' => 'err',
 'error' => "$errstr ($errno)"
 );
 }
 
 // close the socket connection:
 fclose($fp);
 
 // split the result header from the content
 $result = explode("\r\n\r\n", $result, 2);
 
 $header = isset($result[0]) ? $result[0] : '';
 $content = isset($result[1]) ? $result[1] : '';
 
 // return as structured array:
 return array(
 'status' => 'ok',
 'header' => $header,
 'content' => $content
 );
 
}


$databpjs = '{
 "request":
 {
 "t_sep":
 {
 "noSep":"0301R00105160000579",
 "noKartu":"0000015223579",
 "tglSep":"2016-06-12 09:00:00",
 "tglRujukan":"2016-06-12 09:00:00",
 "noRujukan":"00009",
 "ppkRujukan":"00010009",
 "ppkPelayanan":"0301R009",
 "jnsPelayanan":"2",
 "catatan":"test",
 "diagAwal":"A00.1",
 "poliTujuan":"INT",
 "klsRawat":"3",
 "lakaLantas":"2",
 "lokasiLaka":"Banyumas",
 "user":"RS",
 "noMr":"001"
 }
 }
 }';

$data = array(
 'Data' => $databpjs
);


$result = post_request($url, $port, $dataid, $tStamp, $encodedSignature, $databpjs, $referer = '');
if ($result['status'] == 'ok') {
 // Print headers
 echo $result['header'];
 
 echo '<hr />';
 
 //mengubah "re d sponse" menjadi "response"
 $resultstr = str_replace("re d sponse", "response", trim(preg_replace('/\s\s+/', ' ', $result['content'])));

 //mengubah format ke JSON yang valid
 $pos = strpos($resultstr, "{");
 if ($pos === false) 
 $pos = 0;
 $rpos = strrpos($resultstr, "}");
 if ($rpos === false) 
 $rpos = strlen($resultstr);
 $resultstr = substr($resultstr,$pos,$rpos-$pos+1);
 
 // print the result of the whole request:
 echo "<pre>";
 echo $resultstr;
 echo "</pre>";
 
} else {
 echo 'A error occured: ' . $result['error'];
}

?>

2 thoughts on “Contoh bridging BPJS dengan PHP (Update SEP)”

    1. saya tidak paham maksud ‘update field’, contoh di atas hanya untuk koneksi saja, selebihnya silahkan dicoba sendiri

Comments are closed.