本內容為微信開發之微信發送訊息
1,首先,取得開發者測試帳號(申請),會根據目前掃碼提供的帳號產生測試帳號: 連結位址:http:// mp.weixin.qq.com/wiki/home/index.html
#
這時候可以取得到測試用的appid和appsecrept,然後調用取得介面呼叫憑證介面取得access_token;
2,以下說訊息傳送,模擬了單使用者資訊傳送和多使用者訊息批次傳送
(1)基礎方法,http方法
/// <summary> /// http get/post 公用方法 /// </summary> /// <param name="requestUrl">请求链接</param> /// <param name="requestJsonParams">请求参数值(如果是get方式此处为“”值,默认为 "")</param> /// <param name="requestMethod">请求方式 post or get</param> /// <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)模擬發送:
/// <summary> /// 发送微信信息(单用户发送) /// </summary> /// <param name="access_token">授权码(微信token)</param> /// <param name="messageInfo">发送信息模型</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 name="access_token">授权码(微信token)</param> /// <param name="messageInfo">发送信息模型</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)兩個參數模型:
/// <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)web.config中的連結
#
<!--微信接口--> <add key="appid" value="wx123456789021"/> <add key="appSecret" value="adasdsadasdasdsadasdsaqweqw"/> <add key="getAccessTokenUrl" value="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"/> <!--单用户信息发送--> <add key="postSingleTextMessageUrl" value="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}"/> <!--多用户批量发送--> <add key="postTextMessagesUrl" value="https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}"/> <!--\微信接口-->
# 3,測試使用牽涉到 touser的這個參數,這個是需要傳送的物件的 openID,這個很簡單,在開發者文件(也就是上面的步驟二中,)取得
appid 和appsecrept的時候,目前這個頁面下面有一個二維碼,找幾個人用微信掃掃就可以自動獲取openID ,這時候將參數帶入腳本模擬
post即可
另外需注意:文件中提示的 json 參數格式
注意三:token有效時間為7200,兩人小時,需判斷目前發送訊息使用者的token有效性,同時每天最大可請求次數為2000.
取得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
以上是微信開發之微信發送訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!