聊天机器人API Demo

昨天车上无聊,又写起了代码……

居然真写出了个聊天机器人的API Demo……(没啥用)

这个API是调用图灵机器人的官方API,只是简单的解析json,直接把文字信息输出

话不多说,上代码

<?php
$key = "图灵机器人API";
$info = urlencode($_GET['text']);
$ip = $_SERVER["REMOTE_ADDR"];
$userid = ip2long($ip);
$url = "http://www.tuling123.com/openapi/api?key={$key}&info={$info}&userid={$userid}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$output = curl_exec($ch);
curl_close($ch);
$chatArr = json_decode($output);
//print_r($chatArr);
$ChatCode = $chatArr->code;//编码
$ChatText = $chatArr->text;//回答
if($ChatCode == "100000"){
echo $ChatText;
}elseif($ChatCode == "40002"){
echo $ChatText;
}elseif($ChatCode == "200000"){
$ChatUrl = $chatArr->url;//链接
echo $ChatText . "<br/>" . $ChatUrl;
}else{
echo "系统错误  ErrorCode=" . $ChatCode;
}
?>

代码已经托管在GitHub上了,求Star _(:зゝ∠)

Demo: https://www.yuncaioo.com/demo/robotchat

8 条评论

发表评论

*