定义消息类型处理函数
在接收到的消息中根据消息类型,定义不同的消息处理函数,
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 "image": $resultStr = $this->receiveImage($postObj); break; case "location": $resultStr = $this->receiveLocation($postObj); break; case "voice": $resultStr = $this->receiveVoice($postObj); break; case "video": $resultStr = $this->receiveVideo($postObj); break; case "link": $resultStr = $this->receiveLink($postObj); break; case "event": $resultStr = $this->receiveEvent($postObj); break; default: $resultStr = "unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; } }
原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
定义并实现回复函数
在这里,我们先全部用文本消息回复实现
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
各种消息类型函数实现
1、事件消息
private function receiveEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "欢迎关注方倍工作室"; break; case "unsubscribe": $contentStr = ""; break; case "CLICK": switch ($object->EventKey) { default: $contentStr = "你点击了菜单: ".$object->EventKey; break; } break; default: $contentStr = "receive a new event: ".$object->Event; break; } $resultStr = $this->transmitText($object, $contentStr); return $resultStr; }
2、文本消息
private function receiveText($object) { $funcFlag = 0; $contentStr = "你发送的是文本,内容为:".$object->Content; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
3、图片消息
private function receiveImage($object) { $funcFlag = 0; $contentStr = "你发送的是图片,地址为:".$object->PicUrl; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
4、语音消息
private function receiveVoice($object) { $funcFlag = 0; $contentStr = "你发送的是语音,媒体ID为:".$object->MediaId; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
5、视频消息
private function receiveVideo($object) { $funcFlag = 0; $contentStr = "你发送的是视频,媒体ID为:".$object->MediaId; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
6、位置消息
private function receiveLocation($object) { $funcFlag = 0; $contentStr = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
7、链接消息
private function receiveLink($object) { $funcFlag = 0; $contentStr = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; }
完整代码
/* 方倍工作室 http://www.cnblogs.com/txw1958/ */ 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 "image": $resultStr = $this->receiveImage($postObj); break; case "location": $resultStr = $this->receiveLocation($postObj); break; case "voice": $resultStr = $this->receiveVoice($postObj); break; case "video": $resultStr = $this->receiveVideo($postObj); break; case "link": $resultStr = $this->receiveLink($postObj); break; case "event": $resultStr = $this->receiveEvent($postObj); break; default: $resultStr = "unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; } } private function receiveText($object) { $funcFlag = 0; $contentStr = "你发送的是文本,内容为:".$object->Content; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveImage($object) { $funcFlag = 0; $contentStr = "你发送的是图片,地址为:".$object->PicUrl; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveLocation($object) { $funcFlag = 0; $contentStr = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveVoice($object) { $funcFlag = 0; $contentStr = "你发送的是语音,媒体ID为:".$object->MediaId; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveVideo($object) { $funcFlag = 0; $contentStr = "你发送的是视频,媒体ID为:".$object->MediaId; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveLink($object) { $funcFlag = 0; $contentStr = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url; $resultStr = $this->transmitText($object, $contentStr, $funcFlag); return $resultStr; } private function receiveEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "欢迎关注方倍工作室"; break; case "unsubscribe": $contentStr = ""; break; case "CLICK": switch ($object->EventKey) { default: $contentStr = "你点击了: ".$object->EventKey; break; } break; default: $contentStr = "receive a new event: ".$object->Event; break; } $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; }