safeLoad(); } header('Content-Type: application/json'); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['error' => 'Nur POST-Anfragen sind erlaubt.']); exit; } // API-Schlüssel: Erst lokal prüfen, Fallback auf ../home/.env $apiKey = $_ENV['GEMINI_API_KEY'] ?? getenv('GEMINI_API_KEY') ?: ''; if (empty($apiKey) && file_exists(__DIR__ . '/../home/.env')) { foreach (file(__DIR__ . '/../home/.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) { $line = trim($line); if ($line === '' || $line[0] === '#') continue; if (strpos($line, '=') !== false) { list($k, $v) = explode('=', $line, 2); if (trim($k) === 'GEMINI_API_KEY') { $apiKey = trim(trim($v), '"\''); break; } } } } $json_input = file_get_contents('php://input'); $request_data = json_decode($json_input, true); if (json_last_error() !== JSON_ERROR_NONE || !isset($request_data['payload'])) { http_response_code(400); echo json_encode(['error' => 'Ungültige Anfrage-Daten.']); exit; } $payload = $request_data['payload']; // Die Ziel-URL ist jetzt fest im Code hinterlegt und kann nicht von außen manipuliert werden. $googleApiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent'; $fullApiUrl = $googleApiUrl . '?key=' . $apiKey; $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $fullApiUrl, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_HTTPHEADER => ['Content-Type: application/json'], ]); $response_body = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (curl_errno($ch)) { http_response_code(500); echo json_encode(['error' => 'Fehler bei der Weiterleitung: ' . curl_error($ch)]); exit; } curl_close($ch); http_response_code($http_code); echo $response_body; ?>