WeChat reply principle:
When an ordinary WeChat user sends a message to a public account, the WeChat server first receives the message sent by the user;
Then the user information and messages are packaged into a data packet in XML format, and then the XML data packet is submitted to the URL set by the developer through the POST method.
Question 1: Why use $GLOBALS["HTTP_RAW_POST_DATA"] to save the POST data instead of the $_POST array?
Answer:
POST can only save standard data types and cannot parse content such as XML, SOAP or Application/Octet-steam.
$GLOBALS["HTTP_RAW_POST_DATA"] is the same as $_POST. If PHP can recognize the POST data, you can use $GLOBALS["HTTP_RAW_POST_DATA"] to receive it.
Question 2: What are the parameters and return values of simplexml_load_file()?
Answer:
Parameter meaning
string: XML string to be processed.
class: used to specify a new object, usually set to "SimpleXMLElement" to generate a class of simple XML elements.
options: Specify additional Libxml parameters, usually set to the constant LIBXML_NOCDATA, which means setting CDATA as a text node.
ns: generally omitted
is_prefix:generally omitted
After the function is executed, an object of the SimpleXMLElement class is returned.
Function: The official account only accepts text messages and makes corresponding text replies.
<span style="font-family:Courier New;font-size:14px;"><?php define("TOKEN","weixin"); $weixinObj = new Wechat(); $weixinObj->valid(); class Wechat{ public function valid(){ $echoStr = $_GET['echostr']; //如果是第一次接入 if($this->checkSignature() && $echoStr ){ echo $echoStr; exit; }else{ $this->responseMsg(); } } //校验方法 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; } } /* 普通文本消息 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> </xml> */ public function responseMsg(){ //获取微信服务器POST请求中的数据 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if( !empty($postStr) ){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUser = $postObj->FromUserName; $toUser = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; if( strtolower($postObj->MsgType)!='text' ){ $msgType = "text"; $content = "我只接受文本消息"; }else{ $msgType = "text"; if( !empty($keyword) ){ $content = "您发送的消息是:".$postObj->Content; }else{ $content = "请输入关键字";//消息为空 } } $info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); echo $info; }else{ echo ""; exit; } } }</span>
Function: The official account only accepts picture messages and makes corresponding text replies.
<span style="font-family:Courier New;font-size:14px;"><?php define("TOKEN","weixin"); $weixinObj = new Wechat(); $weixinObj->valid(); class Wechat{ public function valid(){ $echoStr = $_GET['echostr']; //如果是第一次接入 if($this->checkSignature() && $echoStr ){ echo $echoStr; exit; }else{ $this->responseMsg(); } } //校验方法 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; } } /* 接收图片消息格式 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[this is a url]]></PicUrl> <MediaId><![CDATA[media_id]]></MediaId> <MsgId>1234567890123456</MsgId> </xml> */ public function responseMsg(){ //获取微信服务器POST请求中的数据 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if( !empty($postStr) ){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUser = $postObj->FromUserName; $toUser = $postObj->ToUserName; $time = time(); $msgType= $postObj->MsgType; $picUrl = $postObj->PicUrl; $mediaId = $postObj->MediaId; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; if( strtolower($msgType)!='image' ){ $msgType = "text"; $content = "我只接受图片消息"; }else{ $msgType = "text"; if( !empty( $picUrl ) ){ $content = "图片链接为:".$picUrl."\n"; $content .= "媒体id:".$mediaId; }else{ $content = "请发送图片";//消息为空 } } $info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); echo $info; }else{ echo ""; exit; } } }</span>
The above is the knowledge that the editor has shared with you about the pitfalls encountered when automatically replying to WeChat messages. I hope it will be helpful to everyone!