PHP的源代码如下,经过测试,完全可以调用,需要用的,可以拿去,有什么问题,随时找智伍技术员交流探讨。
返回的文本内容中,\n表示换行的密码,如果要显示为HTML,要把\n换成<br>
下面的代码本人已经测试过,可以正常运行,能获取到openAI的结果
<?php
set_time_limit(0);
if (!empty($_GET['send']) && $_GET['send'] == 'yes') {
$result = '';
if (empty($_POST['appid'])) {
$result = 'appid不能为空!';
}
if ($result == '' && empty($_POST['appid_key'])) {
$result = '密钥不能为空!';
}
if ($result == '' && empty($_POST['title'])) {
$result = '询问标题不能为空!';
}
if ($result == '') {
$appid = trim($_POST['appid']);
$appid_key = trim($_POST['appid_key']);
$title = trim($_POST['title']);
$t = time();
$signStr = $appid . $title . $t . $appid_key; // 把需要请求的参数串联起来,然后md5加密生成sign字段的数值
$sign = md5($signStr);
$params = array();
$params['appid'] = $appid;
$params['appid_key'] = $appid_key;
$params['title'] = $title;
$params['t'] = $t;
$params['sign'] = $sign;
$paramsString = http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramsString);
curl_setopt($ch, CURLOPT_URL, 'http://api.zhiwu55.net/v1/open_ai/');
$response = curl_exec($ch);
if ($response === FALSE) {
$result = '请求失败了!';
} else {
$httpInfoArr = curl_getinfo($ch);
}
if ($result == '' && $httpInfoArr['http_code'] != 200) {
$result = '请求失败!!错误代码:' . $httpInfoArr['http_code'];
}
if ($result == '' && stripos($response, 'hzw_error_') === false) { // 如果返回的内容含有hzw_error则为出错
$result = nl2br($response);
} else {
$result = '出错:' . str_ireplace('hzw_error_', '', $response);
}
}
}
?>
<!DOCTYPE html>
<html style="font-size:16px;line-height:28px">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="keywords" content="智伍应用">
<meta name="author" content="智伍应用">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,viewport-fit=cover">
<title>ChatGPT接口演示(SDK)</title>
</head>
<body style="font-size:16px;line-height:28px">
<?php if(!empty($result)) { ?>
<div style="background:#DDD;color:red;padding:8px;text-align:center;">
<h2><?php echo $_POST['title']; ?></h2>
<?php echo $result; ?>
</div>
<?php } ?>
<div style="text-align:center;padding-top:32px;">
<a href="http://file.zhiwu55.com/common/apply_open_ai.jpg" target="_blank">如果您没有自己的账号ID和密码,请点击这儿微信扫码免费申请!</a><br><br>
</div>
<form action="?send=yes" method="post">
<table align="center" cellpadding="8">
<tr>
<td colspan="2">
<ul>
<li>无需你申请openAI官方的账号SK密钥,不用担心自己SK密钥被封杀</li>
<li>这个文件是SDK演示,没有任何样式,供用于演示接口的调用,请查看源文件的调用实例代码</li>
<li>切记,警告,请把账号ID和账号密码换成自己的,不要用这个演示账号</li>
<li><a href="http://file.zhiwu55.com/common/apply_open_ai.jpg" target="_blank">微信扫码免费申请自己的专属账号ID和密码</a></li>
</ul>
</td>
</tr>
<tr><td style="text-align:right">演示账号ID:</td><td><input type="text" name="appid" size="64" value="823767debbb383e7977772937b3554c4b9c"></td></tr>
<tr><td style="text-align:right">演示账号密码:</td><td><input type="text" name="appid_key" size="64" value="CLpNiucqvX6Mxhnw4vuOT3BlbkFJG678SSqm6joozrn3kYSV"></td></tr>
<tr><td style="text-align:right">询问内容:</td><td><input type="text" name="title" size="64" value="如何保持健康的身体"></td></tr>
<tr><td colspan="2" style="text-align:center"><input type="submit" value="确定" style="padding:8px 16px;" onClick="this.value='稍等,请求中……';this.disable=true"></td></tr>
</table>
</form>
</body>
</html>