1.根据微信公众平台文档,知道接收普通消息类型如下:
<?php public function ResponseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //获取从微信服务器发送过来的信息 if (!empty($postStr)){ //simplexml_load_string() 函数把 XML 字符串载入对象中。然后从对象中得到相应数据 $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $msgType = $postObj->MsgType; $time = time(); $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>"; $replyMsg = ""; if($msgType == "text") { $replyMsg = "您发的是文本信息"; } else if($msgType == "image") { $replyMsg = "您发的是图片信息"; } else if($msgType == "location") { $replyMsg = "您发的是地理位置信息"; } else if($msgType == "event") { $eventType = $postObj->Event; if($eventType == "subscribe") { $replyMsg = "欢迎你订阅"; } else if($eventType == "CLICK") { $replyMsg = "自定义菜单事件"; } } $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType,$replyMsg); //根据文档要求,组成xml格式 echo $resultStr; //输出,微信服务器接收响应 }else{ echo ""; exit; } } ResponseMsg(); ?>
原文:http://blog.csdn.net/qivan/article/details/19108671