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 in learning it together, this article is a small article about the pitfalls encountered in the automatic reply of the message, which is frequently encountered in daily project development, it is very useful for reference. if you are interested, learn it 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 do I 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.
Valid (); class Wechat {public function valid () {$ echoStr = $ _ GET ['echostr']; // if it is the first access 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.
Valid (); class Wechat {public function valid () {$ echoStr = $ _ GET ['echostr']; // if it is the first access 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 the image message format
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!
The above is a detailed description of the problems encountered during automatic message reply in PHP Development. For more information, see other related articles on php Chinese network!