C#微信公眾平臺開發—高階群發介面
涉及access_token的獲取請參考《C#微信公眾平臺開發—access_token的獲取儲存與更新》
一、為了實現高階群發功能,需要解決的問題
1、通過微信介面上傳圖文訊息素材時,Json中的圖片不是url而是media_id,如何通過微信介面上傳圖片並獲取圖片的media_id?
2、圖片儲存在什麼地方,如何獲取?
二、實現步驟,以根據OpenID列表群發圖文訊息為例
1、準備資料
我把資料儲存在資料庫中,ImgUrl欄位是圖片在伺服器上的相對路徑(這裡的伺服器是自己的伺服器,不是微信的哦)。
從資料庫中獲取資料放到DataTable中:
DataTable dt = ImgItemDal.GetImgItemTable(loginUser.OrgID, data);
2、通過微信介面上傳圖片,返回圖片的media_id
取ImgUrl欄位資料,通過MapPath方法獲取圖片在伺服器上的實體地址,用FileStream類讀取圖片,並上傳給微信
HTTP上傳檔案程式碼(HttpRequestUtil類):
/// <summary> /// Http上傳檔案 /// </summary> public static string HttpUploadFile(string url, string path) { // 設定引數 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線 request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); int pos = path.LastIndexOf("\\"); string fileName = path.Substring(pos + 1); //請求頭部資訊 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] bArr = new byte[fs.Length]; fs.Read(bArr, 0, bArr.Length); fs.Close(); Stream postStream = request.GetRequestStream(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bArr, 0, bArr.Length); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); postStream.Close(); //傳送請求並獲取相應回應資料 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程式才開始向目標網頁傳送Post請求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回結果網頁(html)程式碼 string content = sr.ReadToEnd(); return content; }
請求微信介面,上傳圖片,返回media_id(WXApi類):
/// <summary> /// 上傳媒體返回媒體ID /// </summary> public static string UploadMedia(string access_token, string type, string path) { // 設定引數 string url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", access_token, type); return HttpRequestUtil.HttpUploadFile(url, path); }
string msg = WXApi.UploadMedia(access_token, "image", path); // 上圖片返回媒體ID string media_id = Tools.GetJsonValue(msg, "media_id");
傳入的path(aspx.cs檔案中的程式碼):
string path = MapPath(data);
一個圖文訊息由若干條圖文組成,每條圖文有標題、內容、連結、圖片等
遍歷每條圖文資料,分別請求微信介面,上傳圖片,獲取media_id
3、上傳圖文訊息素材
拼接圖文訊息素材Json字串(ImgItemDal類(操作圖文訊息表的類)):
/// <summary> /// 拼接圖文訊息素材Json字串 /// </summary> public static string GetArticlesJsonStr(PageBase page, string access_token, DataTable dt) { StringBuilder sbArticlesJson = new StringBuilder(); sbArticlesJson.Append("{\"articles\":["); int i = 0; foreach (DataRow dr in dt.Rows) { string path = page.MapPath(dr["ImgUrl"].ToString()); if (!File.Exists(path)) { return "{\"code\":0,\"msg\":\"要傳送的圖片不存在\"}"; } string msg = WXApi.UploadMedia(access_token, "image", path); // 上圖片返回媒體ID string media_id = Tools.GetJsonValue(msg, "media_id"); sbArticlesJson.Append("{"); sbArticlesJson.Append("\"thumb_media_id\":\"" + media_id + "\","); sbArticlesJson.Append("\"author\":\"" + dr["Author"].ToString() + "\","); sbArticlesJson.Append("\"title\":\"" + dr["Title"].ToString() + "\","); sbArticlesJson.Append("\"content_source_url\":\"" + dr["TextUrl"].ToString() + "\","); sbArticlesJson.Append("\"content\":\"" + dr["Content"].ToString() + "\","); sbArticlesJson.Append("\"digest\":\"" + dr["Content"].ToString() + "\","); if (i == dt.Rows.Count - 1) { sbArticlesJson.Append("\"show_cover_pic\":\"1\"}"); } else { sbArticlesJson.Append("\"show_cover_pic\":\"1\"},"); } i++; } sbArticlesJson.Append("]}"); return sbArticlesJson.ToString(); }
上傳圖文訊息素材,獲取圖文訊息的media_id:
/// <summary> /// 請求Url,傳送資料 /// </summary> public static string PostUrl(string url, string postData) { byte[] data = Encoding.UTF8.GetBytes(postData); // 設定引數 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; Stream outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //傳送請求並獲取相應回應資料 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程式才開始向目標網頁傳送Post請求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回結果網頁(html)程式碼 string content = sr.ReadToEnd(); return content; }
/// <summary> /// 上傳圖文訊息素材返回media_id /// </summary> public static string UploadNews(string access_token, string postData) { return HttpRequestUtil.PostUrl(string.Format("https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token={0}", access_token), postData); }
string articlesJson = ImgItemDal.GetArticlesJsonStr(this, access_token, dt); string newsMsg = WXApi.UploadNews(access_token, articlesJson); string newsid = Tools.GetJsonValue(newsMsg, "media_id")
4、群發圖文訊息
獲取全部關注者OpenID集合(WXApi類):
/// <summary> /// 獲取關注者OpenID集合 /// </summary> public static List<string> GetOpenIDs(string access_token) { List<string> result = new List<string>(); List<string> openidList = GetOpenIDs(access_token, null); result.AddRange(openidList); while (openidList.Count > 0) { openidList = GetOpenIDs(access_token, openidList[openidList.Count - 1]); result.AddRange(openidList); } return result; } /// <summary> /// 獲取關注者OpenID集合 /// </summary> public static List<string> GetOpenIDs(string access_token, string next_openid) { // 設定引數 string url = string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}", access_token, string.IsNullOrWhiteSpace(next_openid) ? "" : next_openid); string returnStr = HttpRequestUtil.RequestUrl(url); int count = int.Parse(Tools.GetJsonValue(returnStr, "count")); if (count > 0) { string startFlg = "\"openid\":["; int start = returnStr.IndexOf(startFlg) + startFlg.Length; int end = returnStr.IndexOf("]", start); string openids = returnStr.Substring(start, end - start).Replace("\"", ""); return openids.Split(',').ToList<string>(); } else { return new List<string>(); } }
List<string> openidList = WXApi.GetOpenIDs(access_token); //獲取關注者OpenID列表
拼接圖文訊息Json(WXMsgUtil類):
/// <summary> /// 圖文訊息json /// </summary> public static string CreateNewsJson(string media_id, List<string> openidList) { StringBuilder sb = new StringBuilder(); sb.Append("{\"touser\":["); sb.Append(string.Join(",", openidList.ConvertAll<string>(a => "\"" + a + "\"").ToArray())); sb.Append("],"); sb.Append("\"msgtype\":\"mpnews\","); sb.Append("\"mpnews\":{\"media_id\":\"" + media_id + "\"}"); sb.Append("}"); return sb.ToString(); }
群發程式碼:
resultMsg = WXApi.Send(access_token, WXMsgUtil.CreateNewsJson(newsid, openidList));
/// <summary> /// 根據OpenID列表群發 /// </summary> public static string Send(string access_token, string postData) { return HttpRequestUtil.PostUrl(string.Format("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}", access_token), postData); }
供群發按鈕呼叫的方法(data是傳到頁面的id,根據它從資料庫中取資料):
/// <summary> /// 群發 /// </summary> public string Send() { string type = Request["type"]; string data = Request["data"]; string access_token = AdminUtil.GetAccessToken(this); //獲取access_token List<string> openidList = WXApi.GetOpenIDs(access_token); //獲取關注者OpenID列表 UserInfo loginUser = AdminUtil.GetLoginUser(this); //當前登入使用者 string resultMsg = null; //傳送文字 if (type == "1") { resultMsg = WXApi.Send(access_token, WXMsgUtil.CreateTextJson(data, openidList)); } //傳送圖片 if (type == "2") { string path = MapPath(data); if (!File.Exists(path)) { return "{\"code\":0,\"msg\":\"要傳送的圖片不存在\"}"; } string msg = WXApi.UploadMedia(access_token, "image", path); string media_id = Tools.GetJsonValue(msg, "media_id"); resultMsg = WXApi.Send(access_token, WXMsgUtil.CreateImageJson(media_id, openidList)); } //傳送圖文訊息 if (type == "3") { DataTable dt = ImgItemDal.GetImgItemTable(loginUser.OrgID, data); string articlesJson = ImgItemDal.GetArticlesJsonStr(this, access_token, dt); string newsMsg = WXApi.UploadNews(access_token, articlesJson); string newsid = Tools.GetJsonValue(newsMsg, "media_id"); resultMsg = WXApi.Send(access_token, WXMsgUtil.CreateNewsJson(newsid, openidList)); } //結果處理 if (!string.IsNullOrWhiteSpace(resultMsg)) { string errcode = Tools.GetJsonValue(resultMsg, "errcode"); string errmsg = Tools.GetJsonValue(resultMsg, "errmsg"); if (errcode == "0") { return "{\"code\":1,\"msg\":\"\"}"; } else { return "{\"code\":0,\"msg\":\"errcode:" + errcode + ", errmsg:" + errmsg + "\"}"; } } else { return "{\"code\":0,\"msg\":\"type引數錯誤\"}"; } }
相關文章
- 微信公眾號定時群發平臺
- Senparc.Weixin.MP SDK 微信公眾平臺開發教程(十一):高階介面說明
- 1.PHP微信公眾平臺開發(一) 配置介面PHP
- 微信公眾平臺開發介面PHPSDK完整版PHP
- 微信公眾平臺開發入門
- 微信公眾平臺開發JS-SDK開發(影像介面例項)JS
- Senparc.Weixin.MP SDK 微信公眾平臺開發教程(三):微信公眾平臺開發驗證
- 微信公眾平臺JSSDK分享介面開發(PHP實現)JSPHP
- PHP微信公眾平臺開發視訊PHP
- 微信公眾平臺開發(十一) 功能整合
- 微信公眾平臺完整開發教程 by ZTalk
- 微信公眾平臺介面APIAPI
- 微信公眾平臺開發(六) 翻譯功能開發
- 微信公眾平臺開發之店鋪類
- 基於 PHP 的微信公眾平臺開發PHP
- 微信公眾平臺開發(四) 簡單回覆功能開發
- 微信公眾介面開發介紹
- 2.PHP微信公眾平臺開發(二) 公眾平臺示例程式碼分PHP
- 6.PHP微信公眾平臺開發 - 翻譯功能開發PHP
- 微信公眾平臺開發(五) 天氣預報功能開發
- 微信公眾平臺開發(七) 聊天機器人功能開發機器人
- 微信公眾平臺開發(八) 自定義選單功能開發
- 微信公眾平臺搭建與開發揭祕.pdf
- 微信公眾平臺開發(77) 圖片下載
- 微信公眾平臺開發(九) 資料庫操作資料庫
- 微信公眾平臺通用介面API指南API
- 7.PHP微信公眾平臺開發 聊天機器人開發PHP機器人
- 4.PHP微信公眾平臺開發 - 簡單回覆功能開發PHP
- 9.微信公眾平臺開發 - 資料庫操作資料庫
- ***wechat-php-sdk微信公眾平臺php開發包PHP
- java開發微信公眾平臺(一)-- 伺服器配置Java伺服器
- 微信公眾平臺開發(十二) 傳送客服訊息
- 微信公眾平臺開發(十) 訊息回覆總結
- 微信公眾平臺開發(81) 2014新年微信賀卡
- Senparc.Weixin.MP SDK 微信公眾平臺開發教程(八):通用介面說明
- 移動開發:微信公眾平臺訊息介面-java-jsp版(轉)移動開發JavaJS
- 5.PHP微信公眾平臺開發 - 天氣預報功能開發PHP
- Senparc.Weixin.MP SDK 微信公眾平臺開發教程(十):多客服介面說明