Montag, 25 August 2025

Top 5 diese Woche

Ähnliche Tutorials

Chat GPT einbinden

Hier ist ein einfaches PHP-Codeschnipsel, das zeigt, wie du die OpenAI-ChatGPT-API einbinden kannst. Voraussetzung ist, dass du deinen API-Schlüssel von OpenAI hast.

<?php
$apiKey = 'DEIN_OPENAI_API_SCHLÜSSEL';

$data = [
    'model' => 'gpt-4o-mini',
    'messages' => [
        ['role' => 'user', 'content' => 'Hallo, ich komme von Dreamcodes.net']
    ]
];

$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $apiKey
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Fehler: ' . curl_error($ch);
} else {
    $result = json_decode($response, true);
    echo $result['choices'][0]['message']['content'];
}
curl_close($ch);
?>

Ersetze einfach 'DEIN_OPENAI_API_SCHLÜSSEL' mit deinem tatsächlichen API-Schlüssel. Dieses Beispiel sendet eine einfache Frage an ChatGPT und gibt die Antwort aus.

Vorheriges Tutorial
Nächstes Tutorial

Hier etwas für dich dabei?