$json=file_get_contents('http://www.simsimi.com/func/req?lc=zh&msg='.urlencode($q));
//Unauthorized access!. In this program(site, app), the SimSimi API is being used illegally.
//Please contact us. http://developer.simsimi.com
/**
*
*作者:@Belin_love
*来源:http://52its.sinaapp.com/
*日期:2012.11.27
*
**/
//function simsimi($keyword)
//{
//做成API接口的话,请发起GET请求,返回Josn
//只自己用的话,封装成一个函数,返回JSON字段中的response
if(isset($_GET['key'])){
$keyword = trim($_GET['key']);
$url = "http://www.simsimi.com/talk.htm?lc=ch";
//这个curl是因为官方每次请求都有唯一的COOKIE,我们必须先把COOKIE拿出来,不然会一直返回“HI”
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
list($header, $body) = explode("\r\n\r\n", $content);
preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
//curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$cookie = $matches[1];
$urll = 'http://www.simsimi.com/func/req?msg=' .$keyword. '&lc=ch';
// 这个CURL就是模拟发起请求咯,直接返回的就是JSON
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urll);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, "http://www.simsimi.com/talk.htm?lc=ch");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$content = curl_exec($ch);
curl_close($ch);
//输出json
//print_r($content);
$json = json_decode($content,true);
if (!empty($json) && $json['result']==100){
echo $json['response'];
}
/*$reply = '你说的话太高深啦,我还听不懂,你可以教教我吗?[兔子]';
$json = json_decode($json, 1);
if (isset($json['response'])) {
$reply = $json['response'];
}
echo $reply;
}*/
}
?>
学习用js, ajax, php做一个简单的小黄鸡页面(调用simsimi API):http://www.verydemo.com/demo_c98_i8695.html
function ajaxSimsimi(str){
var xmlhttp;
if (str.length==0){
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){ //XMLHttpRequest对象的有效返回判别
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","curlSim.php?key="+str,true);
xmlhttp.send();
}
请在输入框中键入消息并回车:
求调戏:
http://www.bkjia.com/PHPjc/477669.html www.bkjia.com true http://www.bkjia.com/PHPjc/477669.html TechArticle http://developer.simsimi.com/api SimSimi AICR APIs 1. Conversation API Conversation API enables you to get SimSimis response data. Use the Conversation REST API Request URL: http:/...