WeChat official account developer mode common message mode example-PHP source code

Source: Internet
Author: User
In the common message mode of the Public Account developer mode, we usually use the common message mode, that is, what is then auto-reply. Let's take a look at an example of the common message mode of the Public Account developer mode, the specific operations are as follows. In the common message mode of the Public Account developer mode, we usually use the common message mode, that is, what is then auto-reply. Let's take a look at an example of the common message mode of the Public Account developer mode, the specific operations are as follows.

Script ec (2); script

We have already mentioned how to enable and configure the service number initiator mode. This time, we will talk about some events related to user interaction.

This article mainly describes how to automatically reply a message sent to a service number through the service number input box. In non-initiator mode, you only need to specify the keyword in the auto-reply configuration. The developer mode must be defined by responseMsg or by yourself.

The responseMsg method is the method in the related class when the status is enabled in the first step.

First, let's take a look at the method:

Public function responseMsg (){
$ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"];
If (! Empty ($ postStr )){
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
$ MsgType = $ postObj-> MsgType;
$ FromUsername = $ postObj-> FromUserName;
$ ToUsername = $ postObj-> ToUserName;
$ Keyword = trim ($ postObj-> Content );
$ Time = time ();
$ TextTpl ="
%s
%s
% S
%s
%s
";
$ MsgType = "text ";
$ ContentStr = ', the test is successful. Your information has been reached! ';
$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
Echo $ resultStr; exit ();
} Else {
Exit ();
}
}
The above method is basically a relatively simple method. The following describes the meanings of the preceding important variable values:

$ PostObj the data sent by the user, in xml format
The xml parsing result is as follows:

$ MsgType Message Type
$ FromUsername: openid of the sender
$ ToUsername developer ID
$ Content sent by keyword (note: The text format is used in this example)
$ Time is self-written and sometimes required for business needs.
$ TextTpl is the template style that is returned to the user. There are also several pattern styles
The common message types include text, image, voice, and video. For details about other modes, see the document and event type. The next article will describe them in detail.

The text reply mode is used in this example. For other modes, see the official documentation.

Description of xml parameters:

Use fromUsername to send ToUserName to this user.
For FromUserName, use the ToUserName message sender (developer ID)
CreateTime (timestamp)
MsgType Message Type (reply, Note: text in this example)
Content reply Content can be written as A tag. Control the number of words as much as possible.
After this method is set, enter data in the text box of the service number, and click send. Then, the response is displayed by the service number.

Dear user, the test is successful and your information has been reached!

At this point, the simplest text has been completed. Other common reply operations also follow the same process. Find the corresponding xml type of acceptance and reply, and then perform the operation.

Tip: developers can determine the type of message sent by users in msgType before performing different operations.

For example:

If ($ MsgType = 'text '){}

If ($ MsgType = 'image '){}

If ($ MsgType = 'voice ') {parameters accepted by voice, please refer to the common message class document above. Reply operation: select the xml format of the content you want to reply, please refer to reply to user template style connection .}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.