03-29
0
ChatGPT PHP
1.聊天模型
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.openai.com/v1/chat/completions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode(array(
"model" => "gpt-3.5-turbo",
'max_tokens' => 2000,
"messages" => [array("role" => "user", "content" => $prompt)],
)),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer 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",
),
));
$response = curl_exec($curl);
curl_close($curl);
2.图片生成
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.openai.com/v1/images/generations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode(array(
"prompt" => $prompt,
"n" => 1,
"size" => "1024x1024",
'response_format' => "b64_json",
)),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer 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",
),
));
$response = curl_exec($curl);
curl_close($curl);