php专区

 首页 > php专区 > PHP应用 > 开放平台 > 微信开发基础教程 - 微信公众平台开发:微信公

微信开发基础教程 - 微信公众平台开发:微信公

分享到:
【字体:
导读:
          微信开发学习笔记----1 正确填写服务器配置信息其中Url为我们的要接收并处理微信服务器发送的消息的一般处理程序地址,Token是一个开发者自定义的验证字符串,可任意填写。点击提交...

微信开发学习笔记----

1.正确填写服务器配置信息

其中Url为我们的要接收并处理微信服务器发送的消息的一般处理程序地址,

Token是一个开发者自定义的验证字符串,可任意填写。

点击提交前,需要把咱们的包含一般处理程序的网站发布到服务器上。

2.一般处理程序的编写

 

 

          if (Request.HttpMethod.ToLower() == "get")
            {
                Validate();
            } 

        public void Validate()
        {
                 //微信接口接入验证代码
                string signature = Request["signature"];
                string token = "Your token";
                string timestamp = Request["timestamp"];
                string nonce = Request["nonce"];
                string echostr = Request["echostr"];

                string[] temp = { token, timestamp, nonce };
                Array.Sort(temp);
                string str = string.Join("", temp);
                string sha1Str = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");

                if (sha1Str.ToLower() == signature.ToLower())
                {
                    Response.Write(echostr);
                }
        }

服务器端配置好后,点击“提交”按钮,就会提示成功接入的信息。

3.接收消息

当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。

            else if (Request.HttpMethod.ToLower() == "post")
            {
                //微信服务器发送信息是通过post请求,向发送者以流的形式发送xml
                Stream xmlStream = Request.InputStream;
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlStream);
                XmlElement root = doc.DocumentElement;
                string toUserName = root.SelectSingleNode("ToUserName").InnerText;
                string fromUserName = root.SelectSingleNode("FromUserName").InnerText;
                int createTime = int.Parse(root.SelectSingleNode("CreateTime").InnerText);
                string msgType = root.SelectSingleNode("MsgType").InnerText;
                string content = root.SelectSingleNode("Content").InnerText;
                long msgId = Int64.Parse(root.SelectSingleNode("MsgId").InnerText);
                
            }

     //因为服务器返回的是时间戳,即现在的时间与1970年1月1日8时0分0秒的秒数差,所以可以用此函数对时间进行处理
        public DateTime GetDateTime(int timeSpan)
        {
            return new DateTime(1970,1,1,8,0,0).AddSeconds(timeSpan);
        }

        

4.返回消息

//微信服务器接收信息是通过post请求,向接收者以流的形式发送xml
                /*
                 格式为:
                    
                              
                    
                    12345678
                    
                    
                    
                 */
                string reXml = string.Format(@"
                    
                    
                    {2}
                    
                    
                    ", fromUserName, toUserName, GetSecond(), "已接收到你的消息[服务器自动回复]");
                Response.Write(reXml);     

       public int GetSecond()
          {
              return (int)(DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds;
          }  

现在可以测试一下你的公众号,向公众号发送一个文本消息,公众号在5秒后会自动回复你!

 

分享到:
C#如何校验URL有效性成为微信开发者 - 微...
public string Token = "www0430com";   protected void Page_Load(object sender, EventArgs e)   {     if (string.IsNullOrEmpty(Request.QueryString["echoStr"])) { Response.End(); }     string echoStr = Request.QueryString["echoStr"].ToString();          if (CheckSignature()) ...
微信扫码支付asp.net(C#)实现步骤 - 微...
支付提交页面: [HttpPost] public ActionResult index(decimal amount) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 string order_no = DateTime.Now.ToString("yyyyMMddHHmmss") + TenpayUtil.BuildRandomStr(4); ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……