+
95
-

php的curl如何以steam流的方式请求chatgpt接口?

php的curl如何以steam流的方式请求chatgpt接口?


网友回复

+
15
-

可以使用PHP的curl库以流的方式请求ChatGPT接口,具体步骤如下:

创建一个curl会话,使用curl_init()函数,如下所示:

$ch = curl_init();

设置curl的选项,包括请求的URL、请求方法、请求头、请求体等信息。在这里,我们需要设置请求头Content-Type: application/json,并将请求体转换为json格式,使用json_encode()函数,如下所示:

$url = 'https://api.openai.com/v1/completions';
$data = array(
'prompt' => 'Hello',
'max_tokens' => 5,
'n' => 1,
'stop' => ''
);
$json_data = json_encode($data);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

点击查看剩余70%

我知道答案,我要回答