Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') or exit('No direct script access allowed');
- date_default_timezone_set('UTC');
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\GuzzleException;
- use LZCompressor\LZString;
- class Bpjs extends CI_Controller
- {
- /**
- * @var string
- */
- protected $baseUrl;
- /**
- * @var int
- */
- protected $consumerId;
- /**
- * @var string
- */
- protected $consumerSecret;
- /**
- * @var string
- */
- protected $userKey;
- /**
- * Class constructor
- */
- public function __construct()
- {
- parent::__construct();
- $this->baseUrl = 'https://apijkn-dev.bpjs-kesehatan.go.id/';
- $this->consumerId = 3972;
- $this->consumerSecret = '2wJ5630AAA';
- $this->userKey = 'a931e9e40ac3d692fbdc5bb608ff5707';
- $this->load->database();
- }
- /**
- * @return void
- * @throws GuzzleException
- */
- public function getData()
- {
- $currentTimestamp = strval(time() - strtotime('1970-01-01 00:00:00'));
- $signatureData = "{$this->consumerId}&{$currentTimestamp}";
- $signature = hash_hmac('sha256', $signatureData, $this->consumerSecret, true);
- $signature = base64_encode($signature);
- $headers = [
- 'X-cons-id' => $this->consumerId,
- 'X-Timestamp' => $currentTimestamp,
- 'X-Signature' => $signature,
- 'user_key' => $this->userKey,
- ];
- $client = new Client([
- 'base_uri' => $this->baseUrl,
- 'timeout' => 10,
- ]);
- try {
- $response = $client->get('antreanrs_dev/ref/poli', [
- 'headers' => $headers,
- ]);
- if ($response->getStatusCode() == 200) {
- $responseBody = $response->getBody();
- $content = $responseBody->getContents();
- $data = json_decode($content);
- $response = $data->response;
- $key = "{$this->consumerId}{$this->consumerSecret}{$currentTimestamp}";
- $dataHasilAPI = $this->decode($response, $key);
- return $dataHasilAPI;
- }
- else {
- // error lain
- }
- }
- catch (GuzzleException $e) {
- $responseData = $e->getResponse()->getBody()->getContents();
- $responseCode = $e->getResponse()->getStatusCode();
- if ($responseCode == 403) {
- echo 'User key atau detail akses lain salah.<br>';
- echo 'Balasan server: ' . $responseData;
- }
- else {
- echo $responseBody;
- }
- }
- }
- public function index()
- {
- $data = $this->getData();
- $this->load->view('data', [
- 'data' => $data,
- ]);
- }
- public function simpan()
- {
- $data = $this->getData();
- foreach ($data as $item) {
- $this->db->insert('namatabelnya', [
- 'nmpoli' => $item->nmpoli,
- 'kdsubspesialis' => $item->kdsubspesialis,
- 'kdpoli' => $item->kdpoli,
- ]);
- }
- redirect('bpjs');
- }
- protected function decode($string, $key)
- {
- $encryptMethod = 'AES-256-CBC';
- $keyHash = hex2bin(hash('sha256', $key));
- $iv = substr(hex2bin(hash('sha256', $key)), 0, 16);
- $output = openssl_decrypt(base64_decode($string), $encryptMethod, $keyHash, OPENSSL_RAW_DATA, $iv);
- return json_decode(LZString::decompressFromEncodedURIComponent($output));
- }
- }
Add Comment
Please, Sign In to add comment