index.php file
<form method="POST" action="chat.php">
<input type="text" name="message" placeholder="Ask anything..." required>
<button type="submit">Send</button>
</form>
chat.php file
<?php
$apiKey = "YOUR_API_KEY_HERE";
$userMessage = $_POST['message'];
$data = [
"model" => "gpt-4o-mini",
"messages" => [
["role" => "user", "content" => $userMessage]
]
];
$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);
curl_close($ch);
$result = json_decode($response, true);
echo $result['choices'][0]['message']['content'];
?>
