<?php require_once("config/config.php"); // ข้อมูลที่ต้องการส่งในรูปแบบของตัวแปรอาร์เรย์ (associative array) $data = array( 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'age' => 30, 'officeId' => $officeid ); // แปลงข้อมูลในรูปแบบของคอนเทนต์ JSON $jsonData = json_encode($data); // สร้างคำขอ POST ไปยัง URL และกำหนดข้อมูลที่จะส่งด้วย CURLOPT_POSTFIELDS //$ch = curl_init('https://api.example.com/create-user'); $ch = curl_init($endpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); // กำหนดข้อมูล JSON ในรูปแบบของคอนเทนต์ // กำหนดหัวข้อ Content-Type ในคำขอเพื่อระบุว่าข้อมูลที่ส่งเป็น JSON curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json', 'apiKey:' .$apikey, 'Content-Type: application/json; charset=utf-8' )); // ประมวลผลคำขอและรับข้อมูลตอบกลับจากเซิร์ฟเวอร์ $response = curl_exec($ch); // ตรวจสอบว่ามีข้อผิดพลาดหรือไม่ if (curl_errno($ch)) { echo 'เกิดข้อผิดพลาดในการส่งคำขอ: ' . curl_error($ch); } // ปิดการใช้งานเซสชัน curl curl_close($ch); // แสดงผลลัพธ์ที่ได้รับจาก API echo $response; ?>