This article is related to the pitfalls encountered in the automatic reply of messages introduced by xiaobian. It is frequently encountered in daily project development and has reference value, if you are interested, study together.
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.
<? 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
toUser
fromUser
1348831860
text
this is a test
*/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 ="
%s
%s
% S
%s
%s
"; 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 ;}}}
Function: The public account only accepts image messages and replies accordingly.
<? 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;}/* Format of receiving image messages
toUser
fromUser
1348831860
image
this is a url
media_id
1234567890123456
*/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 ="
%s
%s
% S
%s
%s
"; 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 ;}}}
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!