PHP automatically replies to the pitfalls encountered when messages are automatically replied to in php development.
Reply principle:
When a common user sends a message to a public account, the server first receives the message sent by the user;
Then, package user information and messages into XML data packets, and then submit the XML data packets 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 the $ _ POST array?
Answer:
POST can only store standard data types, but cannot parse content such as XML, SOAP, or Application/Octet-steam.
$ GLOBALS ["HTTP_RAW_POST_DATA"] is the same as $ _ POST. If PHP can identify the POST data, 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 description
String: the XML string to be processed.
Class: used to specify a new object. It is usually set to "SimpleXMLElement" to generate a class for simple XML elements.
Options: Specify the additional Libxml parameter. It is usually set to the constant LIBXML_NOCDATA, which indicates that CDATA is set to 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 public account only accepts text messages and replies accordingly.
<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 is the first access to if ($ this-> checkSignature () & $ echostr) {echo $ echoStr; exit;} else {$ this-> responseMsg () ;}// check method 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 ;}}/* common 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 () {// obtain the data in the server POST request $ 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 = "enter a keyword"; // The message is empty.} $ info = sprintf ($ template, $ fromUser, $ toUser, $ time, $ msgType, $ content); echo $ info;} else {echo ""; exit ;}}</span>
Function: The public account only accepts image messages and replies accordingly.
<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 is the first access to if ($ this-> checkSignature () & $ echostr) {echo $ echoStr; exit;} else {$ this-> responseMsg () ;}// check method 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 ;}}/ * Receives image message formats <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 () {// obtain the data in the server POST request $ 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 image messages";} else {$ msgType = "text"; if (! Empty ($ picUrl) {$ content = "image link :". $ picUrl. "\ n"; $ content. = "Media id :". $ mediaId;} else {$ content = "Please send an image"; // The message is empty.} $ info = sprintf ($ template, $ fromUser, $ toUser, $ time, $ msgType, $ content); echo $ info;} else {echo "; exit ;}}</span>
The above is the knowledge about the pitfalls encountered in the automatic reply of the message shared by xiaobian. I hope it will help you!