微信公众平台入门开发第四章:接收用户消息
定义消息类型处理函数
在接收到的消息中根据消息类型,定义不同的消息处理函数,
public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_D ATA "]; 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 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; } 各种消息类型函数实现 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; } 效果展示: 微信 教材" src="http://img.pintu360.com/wp-content/uploads/2014/03/24214641-d0e553e3ef664776aa1fe127ff57e54e.jpg" width="320" height="480" /> 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; } 效果展示: 品途专栏作者:方倍 责编:杜航