philippurbschat.de/plants/api_proxy.php

60 lines
No EOL
1.7 KiB
PHP

<?php
$current_project = 'plants';
require_once __DIR__ . '/../auth.php';
// Lade die Abhängigkeiten und die .env-Variablen
require_once __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Nur POST-Anfragen sind erlaubt.']);
exit;
}
// Dein geheimer API-Schlüssel wird sicher aus der .env-Datei geladen
$apiKey = $_ENV['GEMINI_API_KEY'];
$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;
?>