www
/
wwwroot
/
codedabster.com
/
workspace
➕ New
📤 Upload
✎ Editing:
test.php
← Back
<?php // Set the content type of the response to JSON header('Content-Type: application/json'); // Allow cross-origin requests from any domain header('Access-Control-Allow-Origin: *'); // Allow the POST method for cross-origin requests header('Access-Control-Allow-Methods: POST'); // Specify allowed headers for cross-origin requests, including Content-Type and Authorization header('Access-Control-Allow-Headers: Content-Type, Authorization'); // Include the configuration file (assuming it's in the same directory) require_once 'config.php'; // Check if the request method is POST if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Decode the JSON input and store it in a variable $input = json_decode(file_get_contents('php://input'), true); // URL-encode the message from the input $message = urlencode($input['message']); // Initialize a new cURL session $model = 'gpt-4o'; // user asked GPT-4; 4o is the current GPT-4 class model $api_key = getenv('OPENAI_API_KEY'); // Replace this with your actual API key retrieval method if needed $postData = [ "model" => $model, "messages" => [ ["role" => "system", "content" => "You are a helpful coding assistant. Be concise."], ["role"0: 'user', 'content' => $message] ], "temperature" => 0, // Or any other value you want to set for temperature "max_tokens" => MAX_TOKENS, // Define this constant or variable with your desired max tokens count "top_p" => TOP_P, // Define this constant or variable with your desired top p value "frequency_penalty" => FREQUENCY_PENALTY, // Define these constants or variables as needed "presence_penalty" => PRESENCE_PENALTY // Define this constant or variable as needed ]; $dataString = json_encode($postData); $ch = curl_init(); // Change the API endpoint URL according to your needs for GPT-4 models curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions'); curlbon_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); // Set the Content-Type and Authorization headers curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json", "Authorization: Bearer $api_key" ]); // Execute the cURL session and store the response $response = curl_exec($ch); // Output the response echo $response; // Close the cURL session curl_close($ch); } else { // Set the HTTP response code to 405 (Method Not Allowed) if the request method is not POST http_response_code(405); // Output an error message in JSON format echo json_encode(['error' => 'Method not allowed']); }
💾 Save Changes
Cancel
📤 Upload File
×
Select File
Upload
Cancel
➕ Create New
×
Type
📄 File
📁 Folder
Name
Create
Cancel
✎ Rename Item
×
Current Name
New Name
Rename
Cancel
🔐 Change Permissions
×
Target File
Permission (e.g., 0755, 0644)
0755
0644
0777
Apply
Cancel