Linux premium92.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64 Path : /home/bajcrwht/api.bajcodigital.com/ |
Current File : /home/bajcrwht/api.bajcodigital.com/index.php |
<?php $postData = $_REQUEST; // Convert the parameters to a JSON string $postDataJson = json_encode($postData, JSON_PRETTY_PRINT); // Define the file path and name /* $filePath = 'api_requests/'; $fileName = 'request_' . date('Y-m-d_H-i-s') . '.txt'; // Ensure the directory exists if (!is_dir($filePath)) { mkdir($filePath, 0777, true); } // Save the JSON string to a text file file_put_contents($filePath . $fileName, $postDataJson);*/ header('Content-Type: application/json'); $servername = "localhost"; $username = "bajcrwht_bajcodigital"; $password = "se8CNal!k{tV"; $dbname = "bajcrwht_bajcodigital"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die(json_encode([ 'success' => false, 'message' => 'Database connection failed: ' . $conn->connect_error, 'data' => [] ])); } // Function to handle the API response function sendResponse($success, $message, $data = [], $statusCode = 200) { http_response_code($statusCode); echo json_encode([ 'success' => $success, 'message' => $message, 'data' => $data ]); exit(); } $requestMethod = $_SERVER["REQUEST_METHOD"]; if ($requestMethod === 'POST') { $ph_avg = isset($_REQUEST['ph_avg']) ? $_REQUEST['ph_avg'] : null; $ph_peak = isset($_REQUEST['ph_peak']) ? $_REQUEST['ph_peak'] : null; $ph_low = isset($_REQUEST['ph_low']) ? $_REQUEST['ph_low'] : null; $tds_avg = isset($_REQUEST['tds_avg']) ? $_REQUEST['tds_avg'] : null; $tds_peak = isset($_REQUEST['tds_peak']) ? $_REQUEST['tds_peak'] : null; $tds_low = isset($_REQUEST['tds_low']) ? $_REQUEST['tds_low'] : null; $turbidity_avg = isset($_REQUEST['turbidity_avg']) ? $_REQUEST['turbidity_avg'] : null; $device_code = isset($_REQUEST['device_code']) ? $_REQUEST['device_code'] : null; // Validate required fields if (!$device_code) { sendResponse(false, 'Device code is required'); } $deviceQuery = "SELECT id FROM devices WHERE device_code = '$device_code' LIMIT 1"; $deviceResult = $conn->query($deviceQuery); if ($deviceResult->num_rows > 0) { $device = $deviceResult->fetch_assoc(); $deviceId = $device['id']; // Insert into clearwater table $insertQuery = "INSERT INTO clearwaters (ph_avg, ph_peak, ph_low, tds_avg, tds_peak, tds_low, turbidity_avg, device_id) VALUES ('$ph_avg', '$ph_peak', '$ph_low', '$tds_avg', '$tds_peak', '$tds_low', '$turbidity_avg', '$deviceId')"; if ($conn->query($insertQuery) === TRUE) { sendResponse(true, 'Clearwater created successfully', [ 'id' => $conn->insert_id, 'ph_avg' => $ph_avg, 'ph_peak' => $ph_peak, 'ph_low' => $ph_low, 'tds_avg' => $tds_avg, 'tds_peak' => $tds_peak, 'tds_low' => $tds_low, 'turbidity_avg' => $turbidity_avg, 'device_id' => $deviceId ]); } else { sendResponse(false, 'Error: ' . $conn->error); } } else { sendResponse(false, 'Device not found'); } } else { sendResponse(false, 'Invalid request method', [], 405); } $conn->close(); ?>