$query ]; // prepare header $api_token = getSettingValue("API_TOKEN"); $headers = [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_token // Add Authorization header ]; // Add variables if provided if ($variables) { $data['variables'] = $variables; } // Encode the data as JSON $dataJson = json_encode($data); // Initialize cURL $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson); // Execute the request and handle errors $response = curl_exec($ch); if ($response === false) { error_log('GraphQL Request Error: ' . curl_error($ch)); return ["error" => "Request failed (GraphQL server might be down). URL: " .$url . " Error: ". curl_error($ch)]; } curl_close($ch); return json_decode($response, true); // Decode and return the JSON response } //------------------------------------------------------------------------------ // Handle incoming requests if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Decode the JSON input from the AJAX request $input = json_decode(file_get_contents('php://input'), true); // Ensure the query is set if (!isset($input['query'])) { echo json_encode(['error' => 'No query provided']); exit; } // Extract the query and variables $query = $input['query']; $variables = isset($input['variables']) ? $input['variables'] : null; // Call the GraphQL function $result = queryGraphQL($query, $variables); // Send the response back to the client header('Content-Type: application/json'); echo json_encode($result); } ?>