网站地图    收藏   

主页 > 后端 > 微信开发 >

微信--获取jsapi_ticket 然后在计算出signature - 微信

来源:自学PHP网    时间:2015-04-14 12:58 作者: 阅读:

[导读] 最近做微信公众号,记录一下,仅供参看。关于jsapi_ticket微信公众号有相关说明:生成签名之前必须先了解一下jsapi_ticket,jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下...

 最近做微信公众号,记录一下,仅供参看。

 

关于jsapi_ticket微信公众号有相关说明:

生成签名之前必须先了解一下jsapi_ticket,jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket 。 

 

Ticket

 public class Ticket
  {
      public string Value { get; set; }
      public DateTime Time { get; set; }
  }

WeixinServer

         #region 静态全局变量
          private static Token _Token = null;
          private static Ticket _Ticket = null;
          private readonly static string appid = "wx123456789000";
          private readonly static string secret = "abcdefghijklmnopqrstuvwxyz";
          private readonly static string domain = "http://wechat.mydomain.com";
         #endregion
          public string GetTicket()
          {
              bool first = false;
              if (_Ticket == null)
              {
                  _Ticket = new Ticket();
                  first = true;
              }
              TimeSpan ts = DateTime.Now - _Ticket.Time;
             if (ts.Minutes > 30 || first)
             {
                 string access_token = AccessToken();
                 string url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";
                 string requstStr = HttpHelper.Get(url1);
                 string ticket = GetParameter("ticket", requstStr);
                 _Ticket.Value = ticket;
                 _Ticket.Time = DateTime.Now;
             }
             return _Ticket.Value;
         }
          //获取jssdk所需签名
          public string Signature(string url)
          {
              string noncestr = "Wm3WZYTPz0wzccnW";
              int timestamp = 1414587457;
              string ticket = GetTicket();
  
              string string1 = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
              string signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string1, "SHA1");
             return signature;
         }

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论