This content is about WeChat sending messages developed by WeChat
1. First, obtain the developer test account (application). The test account will be generated based on the account provided by the current scan code: Link address: http:// mp.weixin.qq.com/wiki/home/index.html
At this time, you can get the appid and appsecret for testing, and then call Get the interface and call the credential interface to get the access_token;
2, let’s talk about information sending, which simulates single-user information sending and multi-user message batch sending
(1) Basic method, http method
/// <summary> /// http get/post 公用方法 /// </summary> /// <param>请求链接 /// <param>请求参数值(如果是get方式此处为“”值,默认为 "") /// <param>请求方式 post or get /// <returns></returns> public static string Request(this string requestUrl, string requestMethod, string requestJsonParams = "") { string returnText = ""; StreamReader streamReader = null; HttpWebRequest request = null; HttpWebResponse response = null; Encoding encoding = Encoding.UTF8; request = (HttpWebRequest)WebRequest.Create(requestUrl); request.Method = requestMethod; if (!string.IsNullOrEmpty(requestJsonParams) && requestMethod.ToLower() == "post") { byte[] buffer = encoding.GetBytes(requestJsonParams); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); } try { response = (HttpWebResponse)request.GetResponse(); using (streamReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312")))//utf-8 { returnText = streamReader.ReadToEnd(); } } catch (Exception ex) { returnText = ex.Message; } return returnText; }
(2) Simulation transmission:
/// <summary> /// 发送微信信息(单用户发送) /// </summary> /// <param>授权码(微信token) /// <param>发送信息模型 /// <returns></returns> public static string SendSingleMessage(WeChatParamEntity messageInfo, string access_token) { messageInfo.MsgType = string.IsNullOrEmpty(messageInfo.MsgType) ? "text" : messageInfo.MsgType; string jsonDataParams = messageInfo == null ? "" : JsonConvert.SerializeObject(new { touser = messageInfo.ToUser, msgtype = messageInfo.MsgType, text = new { content = messageInfo.Text } }); string requestUrl = string.Format(Consts.URL_POSTSINGLETEXTMESSAGE, access_token); return requestUrl.Request("POST", jsonDataParams); } /// <summary> /// 发送微信信息(多用户批量发送) /// </summary> /// <param>授权码(微信token) /// <param>发送信息模型 /// <returns></returns> public static string SendMessages(WeChatParamsEntity messageInfo, string access_token) { messageInfo.MsgType = string.IsNullOrEmpty(messageInfo.MsgType) ? "text" : messageInfo.MsgType; string jsonDataParams = messageInfo == null ? "" : JsonConvert.SerializeObject(new { touser = messageInfo.ToUser, msgtype = messageInfo.MsgType, text = new { content = messageInfo.Text } }); string requestUrl = string.Format(Consts.URL_POSTTEXTMESSAGES, access_token); return requestUrl.Request("POST", jsonDataParams); }
(3) Two parameter model:
/// <summary> /// 微信 发送信息 参数实体模型 /// </summary> public class WeChatParamEntity { /// <summary> /// 普通用户openid /// </summary> public string ToUser { get; set; } /// <summary> /// 传输的文件类型(text,image, and so on) /// </summary> public string MsgType { get; set; } = "text"; /// <summary> /// 传输文本内容 /// </summary> public string Text { get; set; } } /// <summary> /// 微信 发送信息 参数实体模型 /// </summary> public class WeChatParamsEntity { /// <summary> /// 普通用户openid /// </summary> public string[] ToUser { get; set; } /// <summary> /// 传输的文件类型(text,image, and so on) /// </summary> public string MsgType { get; set; } = "text"; /// <summary> /// 传输文本内容 /// </summary> public string Text { get; set; } }
(4) Link in web.config
<!--微信接口--> <add></add> <add></add> <add></add> <!--单用户信息发送--> <add></add> <!--多用户批量发送--> <add></add> <!--\微信接口-->
3. The test uses this parameter involving touser. This is the openID of the object that needs to be sent. This is very simple. Get the
appid and appsecret in the developer documentation (that is, step 2 above). At that time, there is a QR code under the current page. Find a few people to scan it with WeChat to automatically obtain the openID. At this time, enter the parameters into the script to simulate
post
In addition Note: The json parameter format prompted in the document
Note 3: The token is valid for 7200, two hours. It is necessary to determine the validity of the token of the user currently sending the information. At the same time, the maximum number of requests per day is 2000.
Get token:
#region 获取token,并验证token过期时间 public static string GetAccessToken(string appid, string appSecret) { string token = ""; string requestUrl = string.Format(ConfigBLL.URL_GETACCESSTOKEN, appid, appSecret); string requestResult = WebAPITransfer.Request(requestUrl, "GET", ""); CommonBLL.DebugLog(requestResult, "AccessToken-token-Params"); string[] strArray = requestResult.Split(','); token = strArray[0].Split(':')[1].Replace("\"", ""); return token; } #endregion
The above is the detailed content of WeChat development WeChat sends messages. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
