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

900次阅读
没有评论

共计 1089 个字符,预计需要花费 3 分钟才能阅读完成。

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);
?>

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

正文完
 0
评论(没有评论)