ตัวอย่างด้านล่างจะแสดงการใช้งาน API ผ่านโค้ด PHP สองรูปแบบ:
เริ่มต้นด้วยการขอรับ refresh-token และ access-token โดยใช้
username และ password ที่ลงทะเบียนแล้ว
PHP: ด้านล่างเป็นตัวอย่างสคริปต์ PHP ธรรมดาที่ใช้ cURL เพื่อขอรับ token โดยตรงจาก API
Laravel: ใน Laravel ท่านสามารถใช้ Controller เพื่อจัดการ logic การขอรับ token และนำไปใช้ใน Controller อื่น ๆ ได้ ตัวอย่างด้านล่างเป็น Controller สำหรับการขอ token
$username, "password" => $password];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)){
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
return json_decode($response, true);
}
$tokens = getTokens($username, $password);
$refreshToken = $tokens["refresh"];
$accessToken = $tokens["access"];
echo "Refresh Token: " . $refreshToken . "\n";
echo "Access Token: " . $accessToken . "\n";
?>
input('username', 'your_username');
$password = $request->input('password', 'your_password');
$response = $this->callTokenAPI($username, $password);
return response()->json($response);
}
protected function callTokenAPI($username, $password)
{
$url = "https://hcode.moph.go.th/api/token/";
$data = [
"username" => $username,
"password" => $password
];
$client = new \GuzzleHttp\Client();
$res = $client->post($url, [
'json' => $data,
'headers' => [
'Content-Type' => 'application/json',
]
]);
return json_decode((string) $res->getBody(), true);
}
}
?>
PHP: รัน php get_tokens.php บน terminal ท่านจะเห็น refresh และ access token บนหน้าจอ
Laravel: เรียก POST /api/get-tokens (ตามเส้นทางที่ท่านตั้งไว้ใน routes/api.php)
พร้อม username และ password เพื่อรับ tokens
ตัวอย่าง (ด้วย curl):
curl -X POST http://localhost/api/get-tokens \
-H "Content-Type: application/json" \
-d '{"username":"your_username","password":"your_password"}'
ท่านจะได้รับ token ในรูปแบบ JSON
PHP: เมื่อ access-token หมดอายุ (ภายใน 240 นาที)
ท่านสามารถใช้ refresh-token เพื่อขอ access-token ใหม่ ดังตัวอย่างด้านล่าง
Laravel: ใน Controller ท่านสามารถแทรก logic การต่ออายุ token หากการเรียก API ด้วย access-token ปัจจุบันล้มเหลว (401) แล้วจึงเรียกใช้ refresh-token เพื่อขอ access-token ใหม่ โดยอาจเขียนใน Controller เดียวกันหรือแยก logic ออกมา
$refreshToken];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)){
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$json = json_decode($response, true);
return $json["access"] ?? null;
}
$newAccessToken = renewAccessToken($refreshToken);
echo "New Access Token: " . $newAccessToken . "\n";
?>
callHealthOfficeAPI($this->accessToken);
if ($data['status'] === 401) {
// access-token หมดอายุ ขอใหม่
$newAccess = $this->renewAccessToken($this->refreshToken);
if ($newAccess) {
$this->accessToken = $newAccess;
$data = $this->callHealthOfficeAPI($this->accessToken);
} else {
return response()->json(["error" => "Cannot refresh token"], 401);
}
}
return response()->json($data['body']);
}
protected function callHealthOfficeAPI($accessToken)
{
$url = "https://hcode.moph.go.th/api/health_office/?page_size=10";
$client = new Client();
$res = $client->get($url, [
'headers' => [
'Authorization' => "Bearer $accessToken"
],
'http_errors' => false
]);
return [
'status' => $res->getStatusCode(),
'body' => json_decode((string)$res->getBody(), true)
];
}
protected function renewAccessToken($refreshToken)
{
$url = "https://hcode.moph.go.th/api/token/refresh/";
$client = new Client();
$res = $client->post($url, [
'json' => ['refresh' => $refreshToken],
'headers' => ['Content-Type' => 'application/json'],
'http_errors' => false
]);
if ($res->getStatusCode() === 200) {
$json = json_decode((string)$res->getBody(), true);
return $json['access'] ?? null;
}
return null;
}
}
?>