来源:自学PHP网 时间:2015-04-14 12:38 作者: 阅读:次
[导读] 上一篇说到配置和验证《C 微信开发之旅--准备阶段》下面来实现一下简单的信息回复。也就是接收XML,返回XML可以去看下微信开发文档的说明:http: mp weixin qq com wiki 10 79502792eef98d6e0c6e...
上一篇说到配置和验证《C#微信开发之旅--准备阶段》 下面来实现一下简单的信息回复。 也就是接收XML,返回XML 根据请求的信息类型,创建了一个信息请求类: //微信请求类 public class RequestXML { private String toUserName = String.Empty; /// <summary> /// 公众号 /// </summary> public String ToUserName { get { return toUserName; } set { toUserName = value; } } private String fromUserName = ""; /// <summary> /// 发送方微信号 /// </summary> public String FromUserName { get { return fromUserName; } set { fromUserName = value; } } private String createTime = String.Empty; /// <summary> /// 创建时间 /// </summary> public String CreateTime { get { return createTime; } set { createTime = value; } } private String msgType = String.Empty; /// <summary> /// 信息类型 地理位置:location,文本消息:text,消息类型:image /// </summary> public String MsgType { get { return msgType; } set { msgType = value; } } private String content = String.Empty; /// <summary> /// 信息内容 /// </summary> public String Content { get { return content; } set { content = value; } } private String location_X = String.Empty; /// <summary> /// 地理位置纬度 /// </summary> public String Location_X { get { return location_X; } set { location_X = value; } } private String location_Y = String.Empty; /// <summary> /// 地理位置经度 /// </summary> public String Location_Y { get { return location_Y; } set { location_Y = value; } } private String scale = String.Empty; /// <summary> /// 地图缩放大小 /// </summary> public String Scale { get { return scale; } set { scale = value; } } private String mapInfo = String.Empty; /// <summary> /// 地理位置信息 /// </summary> public String MapInfo { get { return mapInfo; } set { mapInfo = value; } } private String picUrl = String.Empty; /// <summary> /// 图片链接 /// </summary> public String PicUrl { get { return picUrl; } set { picUrl = value; } } private String _event; /// <summary> /// 事件类型 /// </summary> public String Event { get { return _event; } set { _event = value; } } private String eventKey; /// <summary> /// 事件key /// </summary> public String EventKey { get { return eventKey; } set { eventKey = value; } } } 1.我们接收XML信息,然后读取其中的信息保存到RequestXML中,然后根据请求类型,返回对应的信息 /// <summary> /// 接收信息 /// </summary> /// <param name="postStr"></param> public void Handle(String postStr) { XmlDocument doc = new XmlDocument(); doc.LoadXml(postStr); XmlElement rootElement = doc.DocumentElement; XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); RequestXML requestXML = new RequestXML(); requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText; requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText; requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText; requestXML.MsgType = MsgType.InnerText; if (requestXML.MsgType == "text") { requestXML.Content = rootElement.SelectSingleNode("Content").InnerText; } else if (requestXML.MsgType == "location") { requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText; requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText; requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText; requestXML.MapInfo = rootElement.SelectSingleNode("Label").InnerText; } else if (requestXML.MsgType == "image") { requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText; } else if (requestXML.MsgType == "event")//事件 { requestXML.Event = rootElement.SelectSingleNode("Event").InnerText; if (requestXML.Event == "CLICK")//点击事件 { requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; } else if (requestXML.Event == "subscribe")//订阅 { } } ResponseMsg(requestXML); } 上面已经处理好了请求信息,现在就要对请求作出回复,回复的信息格式也是XML。 现在就做一个简单的文本回复,就没有弄一个回复类。 /// <summary> /// 回复消息 /// </summary> /// <param name="weixinXML"></param> private void ResponseMsg(RequestXML requestXML) { String responseContent = String.Empty; if (requestXML.MsgType == "text")//文本 { responseContent = FormatTextXML(requestXML.FromUserName, requestXML.ToUserName, "基本回复\r\n" + DateTime.Now.ToString() + "\r\n" + requestXML.FromUserName); } HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); } 上面就是一个简单的文本回复,如果请求的信息是文本的话,我会返回一句话。FormatTextXML就是把封装要返回的XML。 因为我们在开发文档中可以看见,返回的文本信息的话,格式为: <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[你好]]></Content> </xml> 所以FormatTextXML就这样写了: /// <summary> /// 返回格式化文本XML内容 /// </summary> /// <param name="fromUserName"></param> /// <param name="toUserName"></param> /// <param name="content"></param> /// <returns></returns> private String FormatTextXML(String fromUserName, String toUserName, String content) { return "<xml><ToUserName><![CDATA[" + fromUserName + "]]></ToUserName><FromUserName><![CDATA[" + toUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + content + "]]></Content><FuncFlag>1</FuncFlag></xml>"; }
这样一个简单的文本回复就弄好了! 你发送文本信息给公众号,就能得到一个回复了。 一点积累一点点! |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com