<?php
$dataid = "1234"; //Ganti dengan consumerID dari BPJS
$secretKey = "1234"; //Ganti dengan consumerSecret dari BPJS
$localIP = "192.168.0.1";
$url = "http://".$localIP."/devWSLokalRest/SEP/sep"; //Lihat katalog, jangan sertakan port
$port = 8081; //port url
date_default_timezone_set('UTC');
$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, "POST $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>
<data>
<t_sep>
<noKartu>0001134632924</noKartu>
<tglSep>2016-08-4 11:05:03</tglSep>
<tglRujukan>2016-08-2 11:05:03</tglRujukan>
<noRujukan>12345</noRujukan>
<ppkRujukan>0142U037</ppkRujukan>
<ppkPelayanan>3302191</ppkPelayanan>
<jnsPelayanan>2</jnsPelayanan>
<catatan>dari WS</catatan>
<diagAwal>K04.1</diagAwal>
<poliTujuan>GIG</poliTujuan>
<klsRawat>3</klsRawat>
<lakaLantas>2</lakaLantas>
<user>bpjs</user>
<noMr>1234</noMr>
</t_sep>
</data>
</request>";
$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'])));
// print the result of the whole request:
echo "<pre>";
echo $resultstr;
echo "</pre>";
} else {
echo 'A error occured: ' . $result['error'];
}
?>
Kategori: Web
Contoh bridging BPJS dengan PHP (Cari nama peserta berdasarkan NIK)
<?php
$data = "1234"; //Ganti dengan consumerID dari BPJS
$secretKey = "5678"; //Ganti dengan consumerSecret dari BPJS
$url = "http://api.asterix.co.id/SepWebRest/peserta/nik/"; //Lihat katalog
$nik = "3302014525481245"; //ganti dengan NIK (nomor KTP)
date_default_timezone_set('UTC');
$tStamp = strval(time()-strtotime('1970-01-01 00:00:00'));
$signature = hash_hmac('sha256', $data."&".$tStamp, $secretKey, true);
$encodedSignature = base64_encode($signature);
$urlencodedSignature = urlencode($encodedSignature);
echo "X-cons-id: " .$data ."<br>";
echo "X-timestamp:" .$tStamp ."<br>";
echo "X-signature: " .$encodedSignature."<br>";
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Host: api.asterix.co.id\r\n".
"Connection: close\r\n".
"X-timestamp: ".$tStamp."\r\n".
"X-signature: ".$encodedSignature."\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64)\r\n".
"X-cons-id: ".$data."\r\n".
"Accept: application/json\r\n"
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url.$nik, false, $context);
echo "<br>Respon:";
if ($result === false)
{
echo "Tidak dapat menyambung ke server";
} else {
$resultarr=json_decode($result, true);
$s=$resultarr['response']['start'];
$l=$resultarr['response']['limit'];
$c=$resultarr['response']['count'];
echo "<br>Ditemukan ".$c." data, tampil mulai dari nomor ".$s." hingga nomor ".$l."<br>";
for($i=0;$i<$c;$i++){
echo "<h1>Nama: ".$resultarr['response']['list'][$i]['nama']."</h1>";
}
echo "<pre>";
//print_r($resultarr['response']);
echo "</pre>";
}
?>
Sumber rujukan:http://api.bpjs-kesehatan.go.id
Pemanfaatan curl untuk ‘http request’ (POST dan GET) pada PHP
<?php
if(!isset($_GET['curltype'])){
echo "usage: curlpostget.php?curltype=post or curlpostget.php?curltype=get";
exit(1);
}
function curl_post($url, array $postdata = NULL, array $options = array())
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 460,
CURLOPT_POSTFIELDS => http_build_query($postdata)
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
function curl_get($url, array $getdata = NULL)
{
$params = '';
foreach($getdata as $key=>$value)
$params .= $key.'='.urlencode($value).'&';
$params = trim($params, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.$params );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 70); //7 detik
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt($ch, CURLOPT_HEADER, 0);
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
$url = 'https://latiful.hayat.web.id/test/postgetresponse.php';
$data = array(
'id'=>'application/x-www-form-urlencoded',
'key'=>"! * ' ( ) ; : @ & = + $ , / ? % # [ ]"
);
if($_GET['curltype']=='post'){
$result = curl_post($url,$data);
}
if($_GET['curltype']=='get'){
$result = curl_get($url,$data);
}
echo $result;
?>