首页 > 编程 > PHP > 正文

百度大脑和图灵机器人制作一个简单的自动聊天机器人【PHP代码】

2019-11-10 20:28:08
字体:
来源:转载
供稿:网友

可以和机器人聊天 文字发送消息 网页进行语音回复

这里主要用到了两个接口

一个是百度大脑  用来实现语音合成

一个是图灵机器人用来实现自动聊天

以下是代码分两个版本 版本一是没有语音合成 版本二是加上语音合成功能

php版本最低5.5

版本一:

<?php$api='http://www.tuling123.com/openapi/api';$cont=isset($_POST['con'])?$_POST['con']:'';$p=isset($_POST['p'])?$_POST['p']:'';if($p!=1){    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><div style="width:600px; padding:10px; height:800px; border:1px solid #ccc; margin:15px auto;">    <!-- <form action="jqr.php" method="post"> -->    <div id="odiv" style="height:760px;">';}function send_post($url, $post_data) {  $postdata = http_build_query($post_data);  $options = array(    'http' => array(      'method' => 'POST',      'header' => 'Content-type:application/x-www-form-urlencoded',      'content' => $postdata,      'timeout' => 15 * 60 // 超时时间(单位:s)    )  );  $context = stream_context_create($options);  $result = file_get_contents($url, false, $context);  return $result;}if($p){ $post_data = array(  'key' => 'afdf361909844a6395108f8406a15b02',//请自己设置key  'info'=>$cont,  'userid'=>'456789',//用于辨识用户 测试阶段也可以随意写);$conts=send_post($api, $post_data);$conts=explode('text":"',$conts)[1];$conts=explode('"',$conts)[0];echo '<li style="float:right;width:auto"><img src="">'.$cont.'</li>'.'<li style="float:left; width:100%;">'.$conts.'</li>';}if(!$p){    echo "</div><div style='width:600px; margin:5px auto; ''><input id='b01' type='text' style='height:30px; width:540px;'' name='con'><button id='but' style='height:30px;''>提交</button></div><!-- </form> --></div>";}if(!$p){echo '<script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js" type="text/javascript"></script></script><script>    $("button").click(function(){    txt=$("#b01").val();    $.post("jqr.php",{con:txt,p:1},function(result){    	var htmla=$("#odiv").html() + result;   		var odiv =  document.getElementById("odiv");   		odiv.innerHTML = htmla+"<br>";  	})})</script>';}?>

版本二:

<?php$api='http://www.tuling123.com/openapi/api';$cont=isset($_POST['con'])?$_POST['con']:'';$p=isset($_POST['p'])?$_POST['p']:'';if($p!=1){    echo '<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type=text/CSS>*{list-style:none}</style></head><div style="width:600px; padding:10px; height:800px; border:1px solid #ccc; margin:15px auto;">    <!-- <form action="jqr.php" method="post"> -->    <div id="odiv" style="height:760px;">';}function send_post($url, $post_data) {  $postdata = http_build_query($post_data);  $options = array(    'http' => array(      'method' => 'POST',      'header' => 'Content-type:application/x-www-form-urlencoded',      'content' => $postdata,      'timeout' => 15 * 60 // 超时时间(单位:s)    )  );  $context = stream_context_create($options);  $result = file_get_contents($url, false, $context);  return $result;}if($p){ $post_data = array(  'key' => 'afdf361909844a6395108f8406a15b02',  'info'=>$cont,  'userid'=>'456789',);$conts=send_post($api, $post_data);$conts=explode('text":"',$conts)[1];$conts=explode('"',$conts)[0];echo '<li style="float:right;width:auto">'.$cont.'</li>'.'<li style="float:left; width:100%;" style="width:40px; height:20px line-height:20px;"><audio controls style="width:40px; height:10px;" ><source src="http://hope.tk2a.com/baidu.php?con='.$conts.'" type="audio/ogg"></audio>'.$conts.'</li>';}if(!$p){    echo "</div><div style='width:600px; margin:5px auto; style='width:40px; height:10px;' ''><input id='b01' type='text' style='height:30px; width:540px;'' name='con'><button id='but' style='height:30px;''>提交</button></div><!-- </form> --></div>";}if(!$p){echo '<script src="jquery-1.6.4.min.js" type="text/Javascript"></script></script><script>    $("button").click(function(){    txt=$("#b01").val();    $.post("ltt.php",{con:txt,p:1},function(result){    	var htmla=$("#odiv").html() + result;   		var odiv =  document.getElementById("odiv");   		odiv.innerHTML = htmla+"<br>";  	})})</script>';}?>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表