我正在编写一个简单的 PHP 脚本,没有依赖项来访问 ChatGPT API,但它抛出了一个我不明白的错误:
这是到目前为止的脚本:
$apiKey = "Your-API-Key";
$url = 'https://api.openai.com/v1/chat/completions';
$headers = array(
"Authorization: Bearer {$apiKey}",
"OpenAI-Organization: YOUR-ORG-STRING",
"Content-Type: application/json"
);
// Define messages
$messages = array();
$messages["role"] = "user";
$messages["content"] = "Hello future overlord!";
// Define data
$data = array();
$data["model"] = "gpt-3.5-turbo";
$data["messages"] = $messages;
$data["max_tokens"] = 50;
// init curl
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl); …Run Code Online (Sandbox Code Playgroud)