PHP程序调用chatGPT接口,只要有SK就行

PHP的源代码如下,经过测试,完全可以调用,需要用的,可以拿去,有什么问题,随时找智伍技术员交流探讨。

返回的文本内容中,\n表示换行的密码,如果要显示为HTML,要把\n换成<br>

下面的代码本人已经测试过,可以正常运行,能获取到openAI的结果

<?php
set_time_limit(0);
$ch = curl_init();
$text='这儿输入你的问题';
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/completions");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{
  "model": "text-davinci-003",
  "prompt": "'.$text.'",
  "max_tokens": 2048
}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer sk-**Zjsgz***_zhiwu55com_***AsLeSmw8rE**"; // sk密钥,这儿填写chatGPT接口的密钥
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
echo $text."\r\n\r\n\r\n\r\n";
print_r($response);
echo "\r\n\r\n\r\n\r\n";
$response_data = json_decode($response, true);
if (curl_errno($ch)) {
	
  echo 'Error: ' . curl_error($ch);
  
} else {
 
    $result = array(
        'code'=> 200,
        'msg'=>"获取成功",
        'data'=>array(
            'html'=> $response_data['choices'][0]['text'],
            'title'=>$text
        ),
    );
    echo json_encode($result,320);
    exit();
  
}
curl_close($ch);
?>

图片[1]-PHP程序调用chatGPT接口,只要有SK就行 - 易微帮博客-易微帮博客

© 版权声明
THE END
喜欢就支持一下吧
点赞9
分享
评论 抢沙发

请登录后发表评论