requireAuth(); // This will redirect to login if not authenticated // Configuration $webhookUrl = 'https://hook.us1.make.celonis.com/u8i4yq6rydu8u8g9bfhk0xbajsyckrmj'; $responseFile = 'webhook_response.json'; // Initialize variables $response = null; $error = null; $isAjax = isset($_POST['ajax']) && $_POST['ajax'] == '1'; // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['start_date']) && isset($_POST['end_date'])) { $startDate = $_POST['start_date']; $endDate = $_POST['end_date']; // Prepare the data to send $postData = [ 'start_date' => $startDate, 'end_date' => $endDate ]; // Initialize cURL $ch = curl_init($webhookUrl); // Set cURL options curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Accept: application/json' ]); // Execute request $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Check for errors if (curl_errno($ch)) { $error = 'cURL Error: ' . curl_error($ch); } elseif ($httpCode !== 200) { $error = "HTTP Error: Received status code $httpCode"; } else { // Save response to file file_put_contents($responseFile, $response); $error = null; } curl_close($ch); // If AJAX request, return JSON if ($isAjax) { header('Content-Type: application/json'); if ($error) { echo json_encode(['success' => false, 'error' => $error]); } else { echo json_encode(['success' => true, 'message' => 'Data refreshed successfully']); } exit; } } // Set default dates (last 30 days) $defaultEndDate = date('Y-m-d'); $defaultStartDate = date('Y-m-d', strtotime('-30 days')); ?>