VPS参考测评推荐
专注分享VPS主机优惠信息
衡天云优惠活动
华纳云最新优惠促销活动
jtti最新优惠促销活动

调用微信封装的微信签名包(由微信jssdk签名)的类库

locvps
主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作

本文主要介绍微信封装的调用微信签名包的类库的相关资料,有需要的朋友可以参考一下。

废话不多说,我直接把代码发给你。具体代码如下:

& lt?phpnamespace Home \ Model使用思考\模型;类微信模型扩展模型{ private $ _ token = & amp#39;& amp#39;;//令牌私有$ appidprivate $ appsecretpublic function _ _ construct(){ $ this -》;appid = C(& amp;#39;APPID & amp#39;);//微信官方账号中的AppID $ this -》app secret = C(& amp;#39;APPSECRET & amp#39;);//微信官方账号的秘钥}//调用js-sdk的签名包公共函数getsignpackage(){ $ JSAPI ticket = $ this -》getjsapi ticket();//注意,URL必须是动态获取的,而不是硬编码的。(获取当前网页的URL)$ protocol =(!空($ _ SERVER【& amp;#39;HTTPS & amp;#39;】)& amp& amp$ _ SERVER【& amp;#39;HTTPS & amp;#39;] != = & amp#39;关闭& amp#39;| | $ _服务器【& amp#39;服务器端口。#39;] == 443) ?"https://& quot;:& quothttp://& quot;;$ url = & quot$ protocol $ _ SERVER【HTTP _ HOST】$ _ SERVER【REQUEST _ URI】& quot;;//timestamp $ timestamp = time();//随机字符串获取$ non centr = $ this -& gt;createnoncentr();//这里的参数顺序应该按照键值$ string = $ jsapi ticket = $ jsapi ticket & amp;noncestr = $ nonceStr & amp时间戳= $ timestamp & ampurl = $ url & quot;//生成的字符串为$ signature = sha1($ string)用于签名;$ sign package = array(& quot;appId & quot= & gt$ this -》;appid非中心& quot= & gt$ nonceStr,& quot时间戳& quot= & gt$timestampurl & quot= & gt$url签名& quot= & gt$signaturerawString&quot= & gt$ string);return $ signPackage}//使用会员卡接收的签名包公共函数gethuiyansignpack(){ $ API ticket = $ this -》getApiTicket();//注意,URL必须是动态获取的,而不是硬编码的。(获取当前网页的URL)$ protocol =(!空($ _ SERVER【& amp;#39;HTTPS & amp;#39;】)& amp& amp$ _ SERVER【& amp;#39;HTTPS & amp;#39;] != = & amp#39;关闭& amp#39;| | $ _服务器【& amp#39;服务器端口。#39;] == 443) ?"https://& quot;:& quothttp://& quot;;$ url = & quot$ protocol $ _ SERVER【HTTP _ HOST】$ _ SERVER【REQUEST _ URI】& quot;;//timestamp $ timestamp = time();//随机字符串获取//$ non centr = $ this -》;createnoncentr();//这里的参数顺序要按照键值$ string = $ timestamp的ASCII码升序排序。$ apiticket。汽车id & quot;//card_id是自己创建的会员卡的ID//生成的字符串为$ signature = sha1($ string)用于签名;$ sign package = array(& quot;时间戳& quot= & gt$timestamp签名& quot= & gt$signature、);return $ signPackage}//获取会员卡的API _ ticket公共函数getapticket(){ $ data = JSON _ decode(file _ Get _ contents(“API _ ticket . JSON & quot;));if($ data -》;过期时间& lttime()){ $ access token = $ this -& gt;getAccessToken();$ url = & quothttps://api.weixin.qq.com/cgi-bin/ticket/getticket?类型= wx _卡& ampaccess _ token = $ access token & quot;;$ RES = JSON _ decode($ this -& gt;http get($ URL));$ ticket = $ res-& gt;车票;if($ ticket){ $ data -》;expire _ time = time()+7000;$ data -》;jsapi _ ticket = $ ticket$ FP = fopen(& quot;api _ ticket.json & quot,& quotw & quot);fwrite(FP,JSON _ encode(data));fclose($ FP);} } else { $ ticket = $ data -& gt;jsapi _ ticket} return $ ticket}//获取随机字符串私有函数createnoncentr($ length = 16){ $ chars =“abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz 0123456789 & quot;;$ str = & quot";for($ I = 0;$ i & lt$ length$ i++){ $ str。= substr($ chars,mt _ rand(0,strlen($ chars)-1),1);} return $ str}//Get访问令牌公共函数getaccesstoken(){//将json字符串转换为json对象(json_encode是将数组转换为json字符串,JSON _ decode(& quot;",true)如果添加了true,则json字符串被转换为php数组,如果未添加true,则被转换为PHP对象)$ data = JSON _ decode(file _ get _ contents(“access _ token . JSON & quot;));if($ data -》;过期时间& lttime()){//如果是企业,请使用以下URL获取access _ token $ URL =“https://api.weixin.qq.com/cgi-bin/token?格兰特类型=客户端凭据& ampappid = $ this -& gt;appid & ampsecret = $ this -& gt;appsecret"$ RES = JSON _ decode($ this -& gt;http get($ URL));$ access _ token = $ res-& gt;access _ tokenif($ access _ token){ $ data -》;expire _ time = time()+7000;$ data -》;access _ token = $ access _ token$ FP = fopen(& quot;access _ token.json & quot,& quotw & quot);fwrite(FP,JSON _ encode(data));fclose($ FP);} } else { $ access _ token = $ data -& gt;access _ token} return $ access _ token}//Get JSAPI _ ticket(JSAPI _ ticket是微信官方账号用来调用微信JS接口的临时ticket)私有函数getJSAPI ticket(){//JSAPI _ ticket应该全局存储和更新。作为示例,应将以下代码写入文件中:$ data = JSON _ decode(file _ get _ contents(“jsapi _ ticket . JSON & quot;));if($ data -》;过期时间& lttime()){ $ access token = $ this -& gt;getAccessToken();//如果是企业号码,请使用以下URL获取票证//$ URL =“https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?访问令牌= $访问令牌& quot;$ url = & quothttps://api.weixin.qq.com/cgi-bin/ticket/getticket?类型= jsapi & ampaccess _ token = $ access token & quot;;$ RES = JSON _ decode($ this -& gt;http get($ URL));$ ticket = $ res-& gt;车票;if($ ticket){ $ data -》;expire _ time = time()+7000;$ data -》;jsapi _ ticket = $ ticket$ FP = fopen(& quot;jsapi _ ticket.json & quot,& quotw & quot);fwrite(FP,JSON _ encode(data));fclose($ FP);} } else { $ ticket = $ data -& gt;jsapi _ ticket} return $ ticket}//获取用户的OpenID公共函数OpenID(){ $ URL = $ _ server【&;#39;HTTP _ HOST & amp#39;] .$ _ SERVER【& amp;#39;请求_ URI & amp;#39;];如果(!isset($ _ GET【& amp;#39;代码&amp。#39;】){//获取组装的URL $ openidUrl = $ this -& gt;snsapi _ base($ URL);重定向($ openidUrl);} else { $ OpenID access _ token = $ this -& gt;OpenID access _ token($ _ GET【& amp;#39;代码&amp。#39;]);返回$ openidAccess _ token} }//获取微信用户的OPNID公共函数Get OpenID($ OpenID,$ access _ token){ $ userinfo = $ this -》Get userinfo($ OpenID,$ access _ token);return $ userInfo}公共函数snsapi _ base($ redirect _ uri,$ scope = & quotsnsapi _ userinfo & quot,$ state = 0){ $ appId = $ this -& gt;appid$ url = & quothttps://open.weixin.qq.com/connect/oauth2/authorize";$url。= & quot?appid = $ appId & quot;$url。= & quot& amp重定向uri = http://$ redirect _ uri & quot;;$url。= & quot& ampresponse_type=code"$url。= & quot& ampscope = $ scope & quot;$url。= & quot& ampstate = $ state #微信重定向& quot;返回$ url}公共函数OpenID access _ token($ code){ $ appId = $ this -& gt;appid$ app secret = $ this -& gt;appsecret$ url = & quothttps://api.weixin.qq.com/sns/oauth2/access_token? appId = $ appId & amp;secret = $ appSecret & ampcode = $ code & ampgrant _ type = authorization _ code & quot;返回JSON _ decode($ this -》;http get($ URL),true);}//获取用户信息公共函数Get userinfo($ OpenID,$ access _ token){ $ URL =“https://api.weixin.qq.com/sns/userinfo? access _ token = $ access _ token & amp;openid = $ openid & amplang = zh _ CN & quot;返回JSON _ decode($ this -》;http get($ URL),true);//request}私有函数http get($ URL){ $ curl = curl _ init();curl _ setopt($ curl,CURLOPT_RETURNTRANSFER,true);curl _ setopt($ curl,CURLOPT_TIMEOUT,500);curl _ setopt($ curl,CURLOPT_SSL_VERIFYPEER,false);curl _ setopt($ curl,CURLOPT_SSL_VERIFYHOST,false);curl _ setopt($ curl,CURLOPT_URL,$ URL);$ RES = curl _ exec($ curl);curl _ close($ curl);return $ res}}

这几篇文章你可能也喜欢:

本文由主机参考刊发,转载请注明:调用微信封装的微信签名包(由微信jssdk签名)的类库 https://zhujicankao.com/107097.html

【腾讯云】领8888元采购礼包,抢爆款云服务器 每月 9元起,个人开发者加享折上折!
打赏
转载请注明原文链接:主机参考 » 调用微信封装的微信签名包(由微信jssdk签名)的类库
主机参考仅做资料收集,不对商家任何信息及交易做信用担保,购买前请注意风险,有交易纠纷请自行解决!请查阅:特别声明

评论 抢沙发

评论前必须登录!