forked from HAWK-Digital-Environments/HAWKI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
50 lines (40 loc) · 969 Bytes
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
session_start();
if (file_exists(".env")){
$env = parse_ini_file('.env');
}
$apiKey = isset($env) ? $env['OPENAI_API_KEY'] : getenv('OPENAI_API_KEY');;
if (!isset($_SESSION['username'])) {
http_response_code(401);
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$url = 'https://api.openai.com/v1/chat/completions';
$data = file_get_contents("php://input");
$headers = array(
"Authorization: Bearer $apiKey",
'Accept: application/json',
'Content-Type: application/json'
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
// Process the response
if ($response === false) {
echo json_encode(['error' => curl_error($curl)]);
http_response_code(500);
exit;
} else {
echo $response;
exit;
}
}
?>