一个有趣的接口--小黄鸡聊天机器人

浏览:19842 发布日期:2013/12/31 分类:用法示例 关键字: 小黄鸡 api 聊天机器人
哈哈哈哈,小黄鸡接口,可以拿来自娱自乐一下。。。

php端代码如下<?php
    $msg = $_GET['text'];
    //URL中的参数:
    // sandbox.api.simsimi.com/request.p 是试用账号的API
    // key : 用户秘钥,这里是试用秘钥100次请求/天
    // ft : 是否过滤骂人的词汇
    // lc : 语言设置
    // text : 发送信息
    $url = 'http://sandbox.api.simsimi.com/request.p?key=df3c679b-f20a-4bdc-9592-c8730169fa32&ft=0.0&lc=ch&text='.$msg;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20120101 Firefox/17.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
    $res = curl_exec($ch);
    curl_close($ch);
    echo $res;
?>
前端代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>Simsimi</title>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <style type="text/css">
            * {margin:0px;padding:0px;}
            #body {width:500px;}
            #talkbox {border:1px olive solid; width:500px; min-height:100px; float:left; padding:10px;}
            #input {float:right; margin-top:20px}
            #in {height:24px; width:300px}
            #send {background:rgb(51,153,255); border:0; border-radius:5px; height:24px; width:60px; color:white; font-size:14px; font-family:微软雅黑;}
            .left {float:left; clear:both}
            .right {float:right; clear:both}
            .msg {min-height:30px; max-width:300px; line-height:30px; margin-left:20px; margin-bottom:10px; border-radius:6px; border:1px solid #383838; padding:5px; float:left}
            .pic {min-height:40px; width:40px; border:0;float:left}
        </style>
    </head>
    <body>
        <div id="body">
            <div id="talkbox">
                
            </div>
            <div id="input">
                <input id="in" type="text" />
                <button id="send" onclick="send()">发送</button>
            </div>
        </div>
        <script>
            document.onkeydown = function (e) {     //回车发送信息
                var theEvent = window.event || e; 
                var code = theEvent.keyCode || theEvent.which; 
                if (code == 13) { 
                    send();
                } 
            }
            function send(){
                msg = $("#in").val();    //获得输入框中的值
                if(!msg){
                    return false;
                }
                $('#in').val('');    //点击发送后清除表单中的值
                d = $("#talkbox");
                ele = '<div class="left"><div class="pic"><img src="./mi.png" width="40" height="40" /></div><div class="msg">你:'+msg+'</div></div>';
                var simsimi = '';
                d.append(ele);        //将输入框中的值放入聊天框
                $.get("./request.php",{text:msg},function(data){    //将要发的信息ajax提交到php页面
                    var obj = eval('('+data+')');            //php段请求接口,返回来json数据 obj.response是小黄鸡回复的
                    if(obj.result != 100){
                        alert('运行错误');
                    }else{
                        simsimi = '<div class="right"><div class="pic"><img src="./sim.png" width="40" height="40" /></div><div class="msg">小黄鸡:'+obj.response+'</div></div>';
                        d.append(simsimi);
                    }
                })
            }
        </script>
    </body>
</html>
因为存在跨域问题,这里用的ajax+后台请求接口实现

附件 聊天机器人.rar ( 56.27 KB 下载:341 次 )

评论( 相关
后面还有条评论,点击查看>>