The content of this article is about automatic text reply in PHP WeChat development. It has certain reference value. Now I share it with you. Friends in need can refer to it
Today I would like to share with you the development of the WeChat automatic reply function. This time you need to prepare your own server (which can be accessed from the Internet), and carry out it on the public account
Server authentication and enable server configuration.
When users send messages to the official account, WeChat will send these messages to the development team in xml format url corresponding to the developer's server;
The developer receives xmlAfter the message is sent, it can be parsed, and then the corresponding content is sent back to the user according to the content of the message. The reply message must also end with ## Sent in #xml format
.
<xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName> <FromUserName>< ![CDATA[fromUser] ]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[你好] ]></Content> </xml>
Parameter | Is it required | Description |
---|---|---|
ToUserName | is the | receiver account (received OpenID) |
FromUserName | is the | developer WeChat ID |
CreateTime | is the | message creation time (integer) |
MsgType | is | text |
Content | is the message content of | (line break: line breaks can be made in content, WeChat The client supports newline display) |
One thing to note here is that the waiting time for WeChat to send a request to the developer server is 5 seconds. If the developer If the server 5 cannot reply within seconds,
WeChat will resend the request(up to three times). After three times, Still cannot reply within 5 seconds or the content of the reply cannot be parsed by WeChat, and it will display "This official account cannot be
" Serve". Ifcannot guarantee a reply within 5 seconds, you can reply with an empty string and WeChat will not do any processing on this message.
Because the types of messages sent by WeChat to the developer server are relatively diverse, including ordinary messages, follow events, unfollow events, button click events, etc. Therefore, when designing the automatic reply function, the flexibility, scalability and maintainability of the program must be fully considered.Here I used the "chain of responsibility design pattern" to define a processing interface and let each message handler implement this interface; when receiving a request, send the request
The request is passed to the first handler class. Each request class contains a reference to the next handler class; if the request can be processed in this class, it will return to processing directly
The result, otherwise flows to the next handler class until the request is processed. The characteristic of this mode is that it decomposes the steps of processing requests and can make complex judgments
条件进行分解,同时每一个处理程序都只有一个单一的职责,对其进行修改不会影响到其他处理程序类。另外,将每一个请求类
以xml文件的格式配置好,应用程序启动的时候,使用反射+IOC注入的方式实例化每一个处理程序类。
首先创建一个页面,replyText.html
我们创建两个数据表,
rule表 :用来存储回复数据,id自增长 , mp_id是当前正在使用的公众号,keyword是用户输入的关键字,type在此为text,reply_id与reply_text表建立连接,status为当前状态(是否正在使用)。
reply_text表 : reply_id作为主键,content为回复内容。
(在此注意一点,在页面输入对应值后,要把数据统一添加到两个数据表中,add()方法成功返回主键值,可利用这点进行两表关联添加)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
之前介绍过,我用的laneWeChat包,可以直接调用里边的方法,在wechatrequest.lib.php里的text方法中加入以下代码进行文本回复:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
代码要一一写的话就有些多了,在此,只给小伙伴们分享以上代码,如果还有其他问题,欢迎留言提问哦~
请大家多多关注,我会时刻更新的!
相关推荐:
PHP WeChat development to obtain city weather
The above is the detailed content of PHP WeChat development text automatic reply. For more information, please follow other related articles on the PHP Chinese website!