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 package the user information and messages into XML format data packets, and then submit the XML data packet to the URL set by the developer through the POST method.
Question 1:Why use $GLOBALS["HTTP_RAW_POST_DATA"] to save POST data instead of $_POST array?
answer:
POST can only save standard data types, but cannot be parsed for content like XML, SOAP, or Application/Octet-steam.
$GLOBALS["HTTP_RAW_POST_DATA"] and $_POST are the same. If the PHP of the POST can be recognized, it can be received using $GLOBALS["HTTP_RAW_POST_DATA"].
Question 2:What are the parameters and return values of simplexml_load_file()?
answer:
Parameter meaning
string: XML string that needs to be processed.
class: used to specify a new object, usually set to "SimpleXMLElement", to generate a class with simple XML elements.
options: Specify the additional Libxml parameter, 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, it returns an object of the SimpleXMLElement class.
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 it's the first time you accessif($this->checkSignature() && $echoStr ){ echo $echoStr; exit; }else{ $this->responseMsg(); } } //Calibration methodprivate 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; } } /* Normal text message <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(){ //Get the data in the POST request of the WeChat server$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 = "I only accept text messages"; }else{ $msgType = "text"; if( !empty($keyword) ){ $content = "The message you sent is:".$postObj->Content; }else{ $content = "Please enter keywords";//The message is empty} } $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 it's the first time you accessif($this->checkSignature() && $echoStr ){ echo $echoStr; exit; }else{ $this->responseMsg(); } } //Calibration methodprivate 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; } } /* Receive picture message format <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(){ //Get the data in the POST request of the WeChat server$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 = "I only accept picture messages"; }else{ $msgType = "text"; if( !empty( $picUrl ) ){ $content = "The image link is:".$picUrl."\n"; $content .= "Media id:".$mediaId; }else{ $content = "Please send a picture";//The message is empty} } $info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content); echo $info; }else{ echo ""; exit; } } }</span>
The above is the relevant knowledge about the pitfalls encountered when I automatically reply to WeChat messages shared by the editor. I hope it will be helpful to everyone!