微信被动回复用户消息功能——关注、取消
参考资料:http://mp.weixin.qq.com/wiki/1/6239b44c206cab9145b1d52c67e6c551.html
注意:微信公众号必须开启开发者模式才能使用
代码:
<?php namespace Mob\Controller; use Think\Controller; class WxReplayController extends Controller{ protected function _initialize() { /* 读取站点配置 */ $config = api('Config/lists'); C($config); //添加配置 // define("TOKEN", "zkfhao2014"); // 深圳小喇叭 define("TOKEN", C('WX_TOKEN')); } function index(){ if(isset($_GET["echostr"])){ $this->valid(); } $this->responseMsg(); } // ================================== // = 微信开发者模式开启 验证方法 = // ================================== function valid(){ $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg(){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $event = $postObj->Event; $id = $postObj->MsgId; //消息id,64位整型 $fromUsername = $postObj->FromUserName; // openid $toUsername = $postObj->ToUserName; //开发者微信号 $keyword = trim($postObj->Content);//消息内容 $time = $postObj->CreateTime; //消息创建时间 (整型) $msgType = $postObj->MsgType; //text/image/link/music/news/location/...类型 switch ($msgType){ //接收到某个事件 case 'event' : //关注 (取消unsubscribe) if($event == 'subscribe'){ if (isset($postObj->EventKey)){ $cont = "关注二维码场景 ".$postObj->EventKey.$toUsername.'呢称:'.$fromUsername; echo $result = $this->_response_multiText($postObj,$cont); } // 新闻列表消息 $record = array(); $paramArr = array("open_id"=>end($fromUsername)); $res = post_return($paramArr, 'NewUser', 'getuseropenid',"2"); $paramArr = array('uid'=>max(0,$res['data']['uid']),'type'=>1); $goods_res = post_return($paramArr, 'Player', 'get_user_zhubo_list',"2"); $goods_info = $goods_res['data']['lst']; $goods_info = end($goods_info); // 如果有关注的直播间 if( max(0,$goods_res['data']['total']) > 0 ){ $record[]=array( 'title' =>$goods_info['gname'], 'description' =>$goods_info['gname'], 'picUrl' => $goods_info['fig'], 'url' =>C('HOOTS_URL').'/Mob/Chat/index_o/gid/'.$goods_info['gid'], ); } echo $result = $this->_response_multiNews($postObj,$record); } // 扫描二维码 if($event == 'SCAN'){ // $cont = "关注二维码场景 ".$postObj; // echo $result = $this->_response_multiText($postObj,$cont); } break ; // 地理位置 case "LOCATION": $cont = "纬度 ".$postObj->Latitude." 经度".$postObj->Longitude; $result = $this->_response_multiText($postObj,$cont); break; // 文本消息 case "LOCATION": $cont = "您输入的是文字类型"; $result = $this->_response_multiText($postObj,$cont); break; // 默认 default: $cont = '你好!'; echo $result = $this->_response_multiText($postObj,$cont); break; } }else { echo ""; 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; } } // ============================ // = 发送文本消息 = // ============================ function _response_multiText($object,$contentStr){ $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), 'text', $contentStr); return $resultStr; } // ======================================== // = 发送图文消息(点击跳转到外链)= // = 图文消息条数限制在8条以内,= // = 注意,如果图文数超过8,则将会无响应 = // ======================================== function _response_multiNews($object,$newsContent){ $newsTplHead = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles>"; $newsTplBody = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $newsTplFoot = "</Articles> <FuncFlag>0</FuncFlag> </xml>"; $bodyCount = count($newsContent); $bodyCount = $bodyCount < 10 ? $bodyCount : 10; $header = sprintf($newsTplHead, $object->FromUserName, $object->ToUserName, time(), $bodyCount); foreach($newsContent as $key => $value){ $body .= sprintf($newsTplBody, $value['title'], $value['description'], $value['picUrl'], $value['url']); } $FuncFlag = 0; $footer = sprintf($newsTplFoot, $FuncFlag); return $header.$body.$footer; } }