博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信消息回复
阅读量:5088 次
发布时间:2019-06-13

本文共 11590 字,大约阅读时间需要 38 分钟。

微信消息回复的实例代码

 

具体的看一下我带的simplexml_load_string()函数的博客解析

(数据的变化类型和数据传输才是数据的流向)

valid(); }else{ $this->getQrcode(); //设定菜单 没有验证信息 $wechatObj->responseMsg(); } // 有值返回,加载页面 // require 'view/index.html'; } } //聊天类 class wechatCallbackapiTest extends CommonController{ //去验证 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 = 'akira'; //将接到的三个再结合成token,做成一个数组 $tmpArr = array($token, $timestamp, $nonce); //对数组进行字典排序 sort($tmpArr, SORT_STRING); //排序过的再合成一个字符串 $tmpStr = implode( $tmpArr ); //通过sha1()进行加密处理 $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } //相应用户的消息.token验证 public function responseMsg(){ //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收的消息写入日志 // $this->logger('R \n'.$postStr); //extract post data if (!empty($postStr)){ //LIBXML_NOCDATA将 CDATA 设置为文本节点 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //获取传过来的数据类型 $msgType = trim($postObj->MsgType); //text image location voice video link switch($msgType){ case 'event': $resultStr = $this->receiveEvent($postObj); break; case 'text': $resultStr = $this->receiveText($postObj); //接收文本消息 break; case 'image': $resultStr = $this->receiveImage($postObj); //接收图片消息 break; case 'location': $resultStr = $this->receiceLocation($postObj); //接收位置信息 break; case 'voice': $resultStr = $this->receiveVoice($postObj); //接收语音消息 break; case 'video': $resultStr = $this->receiveVideo($postObj); //接收视频消息 break; case 'link': $resultStr = $this->receiveLink($postObj); //接收链接消息 break; default: $resultStr = $this->transText('unknow msg type:'.$msgType); } //回复给用户的消息写入日志 //$this->logger('T \n'.$resultStr); //输出消息给用户 echo $resultStr; }else { echo ""; exit; } } //接收事件消息 private function receiveEvent($object){ //临时定义一个变量,不同的事件发生时,给用户反馈不同的内容 $content = ""; //通过用户发过来的不同事件做一个处理 switch($object->Event){ //用户关注 触发事件 case 'subscribe': $content = "欢迎关注akira的测试账号"; //如果用户传来Eventkey事件,则扫描二维码 // $content .= (!empty($object->EventKey))?("\n来自二维码场景".str):; break; //用户取消关注 case 'unsubscribe': $content = "取消关注"; break; } return $this->transText($object,$content); } //接收图片消息 public function receiveImage($object){ $content = array("Media_Id"=>$object->MediaId); $result = $this->transImage($object,$content); return $result; } //接收位置消息 public function receiceLocation($object){ $content = "你发送的位置是,纬度:".$object->Location_X.";经度为:".$object->Location_Y; $result = $this->transText($object,$content); return $result; } //得到语音消息 public function receiveVoice($object){ if(isset($object->Recognition) && !empty($object->Recognition)){ //通过语言识别返回的文件放到fci函数分词后返回数组(多少个词) $text = $this->fci($object->Recognition); $content = "你刚才说的是:".implode(",",$text); $result = $this->transText($object,$content); }else{ $content = array("MediaId"=>$object->MediaId); $result = $this->transVoice($object,$content); } return $result; } //获取视频消息 public function receiveVideo($object){ } //接收链接信息 public function receiveLink($object){ } //接收文本消息 public function receiveText($object){ //接收用户的文本内容,放到keyword这个变量中 $keyword = trim($object->Content); //如果keyword中有文本两个字就回复给用户一个字符串 if(strstr($keyword,'文本')){ $content = '这是一个文本消息'; }elseif(strstr($keyword,"单图文")){ $content = array(); $content[] = array("Title"=>"这是一个标题","Description"=>"1234","PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/XImdqg47OccUzAbS32jSiaM72Tn2wyEvDLELLPoofiaKaeOAb9DC2F4srzBQCO7yqnSFaK8r5iaEQYguAicVj9XFSQ/640?wx_fmt=jpeg&wxfrom=5","Url"=>"http://mp.weixin.qq.com/s?__biz=MzAxNzUxOTczMg==&mid=207890753&idx=1&sn=4d097f8a06f89f940b1428ec6fc822c0#rd"); }elseif(strstr($keyword,"图文") || strstr($keyword,'多图文')){ $content = array(); $content[] = array("Title"=>"这是一个标题1","Description"=>"111","PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/XImdqg47OccUzAbS32jSiaM72Tn2wyEvDLELLPoofiaKaeOAb9DC2F4srzBQCO7yqnSFaK8r5iaEQYguAicVj9XFSQ/640?wx_fmt=jpeg&wxfrom=5","Url"=>"http://mp.weixin.qq.com/s?__biz=MzAxNzUxOTczMg==&mid=207890753&idx=1&sn=4d097f8a06f89f940b1428ec6fc822c0#rd"); $content[] = array("Title"=>"这是一个标题2","Description"=>"222","PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/XImdqg47OccUzAbS32jSiaM72Tn2wyEvDLELLPoofiaKaeOAb9DC2F4srzBQCO7yqnSFaK8r5iaEQYguAicVj9XFSQ/640?wx_fmt=jpeg&wxfrom=5","Url"=>"http://mp.weixin.qq.com/s?__biz=MzAxNzUxOTczMg==&mid=207890753&idx=1&sn=4d097f8a06f89f940b1428ec6fc822c0#rd"); $content[] = array("Title"=>"这是一个标题3","Description"=>"333","PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/XImdqg47OccUzAbS32jSiaM72Tn2wyEvDLELLPoofiaKaeOAb9DC2F4srzBQCO7yqnSFaK8r5iaEQYguAicVj9XFSQ/640?wx_fmt=jpeg&wxfrom=5","Url"=>"http://mp.weixin.qq.com/s?__biz=MzAxNzUxOTczMg==&mid=207890753&idx=1&sn=4d097f8a06f89f940b1428ec6fc822c0#rd"); $content[] = array("Title"=>"这是一个标题4","Description"=>"444","PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/XImdqg47OccUzAbS32jSiaM72Tn2wyEvDLELLPoofiaKaeOAb9DC2F4srzBQCO7yqnSFaK8r5iaEQYguAicVj9XFSQ/640?wx_fmt=jpeg&wxfrom=5","Url"=>"http://mp.weixin.qq.com/s?__biz=MzAxNzUxOTczMg==&mid=207890753&idx=1&sn=4d097f8a06f89f940b1428ec6fc822c0#rd"); }elseif(strstr($keyword,"音乐")){ $content = array("Title"=>"歌曲","Description"=>"nice的一逼","MusicUrl"=>"http://akira.weixin-sandbox.ciicgat.com/public/bcjl.mp3","HQMusicUrl"=>"http://akira.weixin-sandbox.ciicgat.com/public/bcjl.mp3"); }elseif(strstr($keyword,"抽奖")){ $content = $this->get_authorize_url(CJURL,'1'); } else{ $content = "akira".date('Y-m-d H:i:s')."技术支持"; } //是数组,则为图文 if(is_array($content)){ if(isset($content[0])){ //第一张图有PicUrl,为单图文 $res = $this->transNews($object,$content); }elseif(isset($content['MusicUrl'])){ $res = $this->transMusic($object,$content); } }else{ //$keyword 你输入的文本 //$object->FromUserName //调用一个方法,将openid和你输入的文本传入,使用这个函数处理 $this->getUserInfo($object->FromUserName,$keyword); $res = $this->transText($object,$content); } return $res; } //回复语音消息 public function transVoice($object,$voice){ $xmlTpl = "
%s
"; $result = sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),$voice['MediaId']); return $result; } //回复图片消息 public function transImage($object,$image){ $xmlTpl = "
%s
"; $result = sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),$object->MediaId); return $result; } //回复音乐消息 public function transMusic($object,$music){ $xmlTpl = "
%s
<![CDATA[%s]]>
"; $re = sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),$music['Title'],$music['Description'],$music['MusicUrl'],$music['HQMusicUrl']); return $re; } //做单图文的相应消息 public function transNews($object,$newsArray){ if(!is_array($newsArray)){ return ''; } $itemTpl = "
<![CDATA[%s]]>
"; $item_str = ""; foreach($newsArray as $item){ $item_str .= sprintf($itemTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']); } $xmlTpl = "
%s
%s
$item_str
"; return sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),count($newsArray)); } //向用户传输文本 public function transText($object,$content){ $xmlTpl = "
%s
"; $result = sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),$content); return $result; } /*写日志 @param string $log_content 传日志的内容 */ public function logger($log_content){ //日志大小 $max_file_size = 10000; $log_filename = 'log.xml'; if(file_exists($log_filename) && abs(filesize($log_filename)) > $max_file_size){ @unlink($log_filename); } //写入日志,追加写日志 file_put_contents($log_filename,date('H:i:s').' '.$log_content.'\n',FILE_APPEND); } }?>

对于数据抓包的处理很多时候决定了传输的定性

接收普通消息

http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html

接收事件推送

http://mp.weixin.qq.com/wiki/2/5baf56ce4947d35003b86a9805634b1e.html

转载于:https://www.cnblogs.com/examine/p/4685214.html

你可能感兴趣的文章
swift 类和结构体
查看>>
mono支持gb2312
查看>>
SharePoint 数据库迁移步骤
查看>>
Python基础
查看>>
洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
查看>>
字符串占位符的使用
查看>>
mina 通讯框架
查看>>
MySQL分区表的管理~1
查看>>
vue表格业务
查看>>
maven 配置说明
查看>>
js接收网页参数
查看>>
Elasticsearch安装
查看>>
linux之查看端口占用
查看>>
[原创]浅谈互联网金融测试平台规划
查看>>
什么是网关及网关作用(转)
查看>>
skymvc网站测试之mysql数据生成
查看>>
静态内存、动态内存与堆栈
查看>>
Asp.Net Core WebApi 和Asp.Net WebApi上传文件
查看>>
如何在应用图标上显示未读消息
查看>>
android脚步---如何看log之程序停止运行,和UI线程和非UI线程之间切换
查看>>