一、模拟请求数据
先看一下小黄鸡的网页版界面
我们通过模拟http请求来实现,上面对话抓包如下:
发送消息的包
接收消息的包:
根据上面的包,模拟发起请求如下:
二、与微信对接
小黄鸡还可以使用API方式调用,但免费时间有限,代码和上面基本一样,就不多写了。
三、演示效果
第二部分 基于接口的方式一、申请小黄鸡接口SimSimi,发音为〝shim-shimee〞,中文翻译:小黄鸡,由韩文simsim(심심)演变而成。 “심심”原意为“无聊”。SimSimi 由ISMaker于2002年建立流行的人工智能对话系统。官方网站 http://www.simsimi.com/
接口申请地址:http://developer.simsimi.com/pricing接口申请流程:1. 注册账号2. 激活账号3. 申请7天试用接口(或者付费购买接口,价格30美元10 万次)4. 获得Key:d4899e2a-7ea8-40ab-a0d2-f258ddd7de15
二、接口分析
请求URL
http://sandbox.api.simsimi.com/request.p?key=your_trial_key $params['lc'] = "ch"; $params['ft'] = "1.0"; $params['text'] = $keyword; $url = "http://sandbox.api.simsimi.com/request.p?".http_build_query($params);
CURL获取内容
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch);获取回复内容
$message = json_decode($output,true); $result = ""; if ($message['result'] == 100){ $result = $message['response']; }else{ $result = $message['result'].$message['msg']; }小黄鸡完整代码simsimi.php
function callSimsimi($keyword){ $params['key'] = "d4899e2a-7ea8-40ab-a0d2-f258ddd7de15"; $params['lc'] = "ch"; $params['ft'] = "1.0"; $params['text'] = $keyword; $url = "http://sandbox.api.simsimi.com/request.p?".http_build_query($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $message = json_decode($output,true); $result = ""; if ($message['result'] == 100){ $result = $message['response']; }else{ $result = $message['result']."-".$message['msg']; } return $result;}接入微信公众平台将官方文件的$cOntentStr= “Welcome to wechat world!”; 修改为接口调用$cOntentStr= callSimsimi($keyword);
define("TOKEN", "pondbay");$wechatObj = new wechatCallbackapiTest();if (isset($_GET['echostr'])) { $wechatObj->valid();}else{ $wechatObj->responseMsg();}class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nOnce= $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "text": $resultStr = $this->receiveText($postObj); break; case "event": $resultStr = $this->receiveEvent($postObj); break; } echo $resultStr; }else { echo ""; exit; } } private function receiveText($object) { $funcFlag = 0; $keyword = trim($object->Content); include('simsimi.php'); $cOntentStr= callSimsimi($keyword); $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveEvent($object) { $cOntentStr= ""; switch ($object->Event) { case "subscribe": $cOntentStr= "欢迎关注方倍工作室\n小黄鸡为您服务!"; } $resultStr = $this->transmitText($object, $contentStr); return $resultStr; } private function transmitText($object, $content, $flag = 0) { $textTpl = "%s%d"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; }}更多内容,请关注方倍工作室微信,然后回复 “教程”,
微信公众平台消息 接口小黄鸡 小贱鸡 机器人作者:http://www.cnblogs.com/txw1958/