-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.php
More file actions
74 lines (60 loc) · 1.83 KB
/
chat.php
File metadata and controls
74 lines (60 loc) · 1.83 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
$baseTargetUrl = 'https://text.pollinations.ai/';
// 处理GET请求
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$ch = curl_init();
if (isset($_GET['prompt']) && !empty($_GET['prompt'])) {
$targetUrl = $baseTargetUrl . urlencode($_GET['prompt']);
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
} else {
echo $response;
}
} else {
echo "The 'prompt' parameter is missing or empty.";
}
curl_close($ch);
}
// 处理POST请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$ch = curl_init();
$postData = file_get_contents('php://input');
curl_setopt($ch, CURLOPT_URL, $baseTargetUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($postData)
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
} else {
// 封装返回的数据
$data = [
"id" => "chatcmpl-ANJZGVj3VpFxVMDdyIY0U7SOPENAI",
"choices" => [
[
"index" => 0,
"message" => [
"role" => "assistant",
"content" => $response
],
"logprobs" => null,
"finish_reason" => "stop"
]
],
"created" => time(),
"model" => "openai",
"object" => "chat.completion"
];
echo json_encode($data);
}
curl_close($ch);
}
?>