PHP的源代码如下,经过测试,完全可以调用,需要用的,可以拿去,有什么问题,随时找智伍技术员交流探讨。
返回的文本内容中,\n表示换行的密码,如果要显示为HTML,要把\n换成
下面的代码本人已经测试过,可以正常运行,能获取到openAI的结果
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);
}
}
}
?>