来源:自学PHP网 时间:2015-04-14 12:58 作者: 阅读:次
[导读] 微信公众平台消息 接口小黄鸡 小贱鸡 机器人作者:http: www cnblogs com txw1958 第一部分 基于模拟请求的方式一、模拟请求数据先看一下小黄鸡的网页版界面我们通过模拟http请求来实现,上...
第一部分 基于模拟请求的方式 一、模拟请求数据 先看一下小黄鸡的网页版界面 我们通过模拟http请求来实现,上面对话抓包如下: 发送消息的包 接收消息的包: 根据上面的包,模拟发起请求如下:
二、与微信对接
小黄鸡还可以使用API方式调用,但免费时间有限,代码和上面基本一样,就不多写了。 三、演示效果
第二部分 基于接口的方式一、申请小黄鸡接口 接口申请地址: http://developer.simsimi.com/pricing
二、接口分析 请求 URL http://sandbox.api.simsimi.com/request.p?key=your_trial_key&lc=en&ft=1.0&text=hi 请求参数说明 响应示例 响应参数说明 三、程序实现 构造Url参数 $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); 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; } 接入微信公众平台 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 = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; } }
更多内容,请关注方倍工作室微信,然后回复 “教程”, 原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com