微信消息回复
时间:2015-07-29 11:39:20
收藏:0
阅读:983
微信消息回复的实例代码
具体的看一下我带的simplexml_load_string()函数的博客解析
(数据的变化类型和数据传输才是数据的流向)
<?php class IndexController extends CommonController{ //加载首页 public function Index(){ //define your token $wechatObj = new wechatCallbackapiTest(); //判定变量 if(isset($_GET[‘echostr‘])){ //有变量是验证 接口配置 $wechatObj->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 = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[voice]]></MsgType> <Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice> </xml>"; $result = sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),$voice[‘MediaId‘]); return $result; } //回复图片消息 public function transImage($object,$image){ $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[image]]></MsgType> <Image> <MediaId><![CDATA[%s]]></MediaId> </Image> </xml>"; $result = sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),$object->MediaId); return $result; } //回复音乐消息 public function transMusic($object,$music){ $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music> </xml>"; $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 = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach($newsArray as $item){ $item_str .= sprintf($itemTpl,$item[‘Title‘],$item[‘Description‘],$item[‘PicUrl‘],$item[‘Url‘]); } $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles> $item_str </Articles> </xml>"; return sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),count($newsArray)); } //向用户传输文本 public function transText($object,$content){ $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $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
评论(0)