请自行申请OpenAI Key
申请地址:https://beta.openai.com/account/api-keys
- $text="问答内容"; //例:致富论坛网址是多少?
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/completions");
- 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);
- // Set the API key as an HTTP header
- $headers = array();
- $headers[] = "Content-Type: application/json";
- $headers[] = "Authorization: Bearer 你的KEY";
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- // Send the request and parse the response
- $response = curl_exec($ch);
- $response_data = json_decode($response, true);
- if (curl_errno($ch)) {
- // If there was an error executing the cURL request, print it out
- echo 'Error: ' . curl_error($ch);
- } else {
- $result = array(
- 'code'=> 200,
- 'msg'=>"获取成功",
- 'data'=>array(
- 'html'=> trim($response_data['choices'][0]['text']),
- 'title'=>$text
- ),
- );
- echo json_encode($result,320);
- exit();
- }
- curl_close($ch);
复制代码 |