Home > Backend Development > PHP Tutorial > WeChat Public Account Development Tutorial Part 5 - Receiving and Responding to Various Messages_PHP Tutorial

WeChat Public Account Development Tutorial Part 5 - Receiving and Responding to Various Messages_PHP Tutorial

WBOY
Release: 2016-07-20 11:12:16
Original
1015 people have browsed it

In the previous article, we have encapsulated the messages and related operations in the WeChat public platform interface. This chapter will mainly introduce how to receive and respond to messages sent by the WeChat server.

Clear where to receive messages

From the WeChat public platform interface message guide, we can learn that when a user sends a message to a public account, the WeChat server will submit the message to the URL we filled in the interface configuration information through POST, and we need to enter the URL in the interface configuration information. Receive messages, process messages and respond to messages in the doPost method of the pointed request processing class CoreServlet.

Receive, process and respond to messages

Let’s take a look at the complete code of CoreServlet that I have written:

package org.liufeng.course.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.liufeng.course.service.CoreService;
import org.liufeng.course.util.SignUtil;

/**
 * 核心请求处理类
 * 
 * @author liufeng
 * @date 2013-05-18
 */
public class CoreServlet extends HttpServlet {
	private static final long serialVersionUID = 4440739483644821986L;

	/**
	 * 确认请求来自微信服务器
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 微信加密签名
		String signature = request.getParameter("signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");

		PrintWriter out = response.getWriter();
		// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
		if (SignUtil.checkSignature(signature, timestamp, nonce)) {
			out.print(echostr);
		}
		out.close();
		out = null;
	}

	/**
	 * 处理微信服务器发来的消息
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 将请求、响应的编码均设置为UTF-8(防止中文乱码)
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 调用核心业务类接收消息、处理消息
		String respMessage = CoreService.processRequest(request);
		
		// 响应消息
		PrintWriter out = response.getWriter();
		out.print(respMessage);
		out.close();
	}

}
Copy after login

Code description:

1) Line 29: Call the message tool class MessageUtil to parse the xml format message sent by WeChat, and put the parsed result in the HashMap;

2) Lines 32~36: Remove the fields in the message from HashMap;

3) Lines 39-44 and 84: Assemble the text message object to be returned;

4) Lines 47~82: Demonstrates how to receive various types of messages sent by WeChat and determine which type of message it belongs to based on MsgType;

5) Line 85: Call the message tool class MessageUtil to convert the text message object TextMessage to be returned into an xml format string;

About event push (follow, unfollow, menu click)

It is easy to understand the judgment of message types, such as text messages, picture messages, geographical location messages, link messages and voice messages. Many new friends do not understand the use of event push messages, or do not know how to use them. How to determine the news that users care about. Then let’s look specifically at event push. The picture below is the description of event push in the official message interface document:

Here we only need to care about two parameters: MsgType and Event. When MsgType=event, it means that this is an event push message; and Event means the event type, including subscription, unsubscription and custom menu click events. In other words, whether the user is following a public account, unfollowing a public account, or using the menu of a public account, the WeChat server will send us a message with MsgType=event, and as for the specific message indicating following, canceling Whether it is a focus or a click event on the menu needs to be judged by the value of the Event. (Note to distinguish between Event and event)

Summary of five series of tutorials

After 5 articles of explanation, the development mode activation, interface configuration, message-related tool class encapsulation, message reception and response have all been explained, and the complete source code has been posted. I believe that friends with a certain Java foundation It can be seen clearly and you can basically master the relevant technical knowledge of WeChat public platform development through a series of articles. Below I post the complete structure of the current project for everyone’s convenience:


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444572.htmlTechArticleIn the previous article, we have encapsulated the messages and related operations in the WeChat public platform interface. This chapter This will mainly introduce how to receive and respond to messages sent by the WeChat server...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template