主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情! |
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作 |
本文主要介绍调用微信封装的微信签名包的类库的相关信息。有需要的朋友可以参考一下。
废话不多说,把代码贴给大家就行了。具体代码如下:
lt?phpnamespace Home \ Model使用Think \ Model类微信模型扩展模型{ private $ _ token = # 39';//令牌私有$ appidprivate $ appsecretpublic function _ _ construct(){ $ this - gt;appid = C( # 39;APPID # 39);//微信官方账号的appid $ this ->:app secret = C( # 39;APPSECRET # 39);//微信官方账号的秘钥}//调用js-sdk的签名包公共函数getsignpackage(){ $ jsapi ticket = $ this ->;getjsapi ticket();//注意,URL必须动态获取,不能硬编码。$ protocol =(!空($ _ SERVER[ # 39;HTTPS # 39;]) ; $ _服务器[ # 39;HTTPS # 39;] !== '关 # 39;| | $ _服务器[ # 39;服务器端口 # 39;] == 443) ?"https:// ;: http:// ;;$ url = $ protocol $ _ SERVER[HTTP _ HOST]$ _ SERVER[REQUEST _ URI] ;;//timest $ timest = time();//获取$ non centr = $ this ->:createnoncentr()的随机字符串;//这里参数的顺序要按照键值$ string = " jsapi _ ticket = $ jsapi ticket ;noncestr = $ nonceStr 时间戳= $时间戳 url = $ url ;//生成的字符串为$signature = sha1($string)进行签名;$ sign package = array( ;appId = gt$ this - gt;appid, 非中心 = gt$ nonceStr, 时间戳 = gt$timest, url = gt$url, 签名 = gt$signature, rawString = gt$ string);返回$ signPackage}//签名包公共函数gethuiyansignpack(){ $ API ticket = $ this ->;getApiTicket();//注意,URL必须动态获取,不能硬编码。$ protocol =(!空($ _ SERVER[ # 39;HTTPS # 39;]) ; $ _服务器[ # 39;HTTPS # 39;] !== '关 # 39;| | $ _服务器[ # 39;服务器端口 # 39;] == 443) ?"https:// ;: http:// ;;$ url = $ protocol $ _ SERVER[HTTP _ HOST]$ _ SERVER[REQUEST _ URI] ;;//timest $ timest = time();//随机字符串获取//$ non centr = $ this ->;createnoncentr();//这里参数的顺序应该是键值$ string = $ timest的ASCII码升序排序。$ apiticket。汽车id ;//card_id是您创建的会员卡的id。//生成的字符串为$signature = sha1($string)进行签名;$ sign package = array( ;时间戳 = gt$timest, 签名 = gt$signature,);返回$ signPackage}//Get API _ Ticket公共函数GetAPI Ticket(){ $ data = JSON _ DECODE(file _ Get _ contents(" API _ Ticket . JSON ;));if($data - gt;expire _ time lttime()){ $ access token = $ this - gt;getAccessToken();$ url = https://api.weixin.qq.com/cgi-bin/ticket/getticket?类型= wx _卡 access _ token = $ access token ;;$ RES = JSON _ decode($ this - gt;http get($ URL));$ ticket = $ res- gt;票;if($ ticket){ $ data - gt;expire _ time = time()+7000;$data->jsapi _ ticket = $ ticket$ FP = fopen( ;api _ ticket.json , w );fwrite($fp,JSON _ encode($ data));fclose($ FP);} } else { $ ticket = $ data - gt;jsapi _ ticket} return $ ticket}//获取随机字符串私有函数createnoncentr($ length = 16){ $ chars = " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz 0123456789 ;;$ str = ";for($ I = 0;$ i lt$长度;$i++) { $str。= substr($chars,mt_rand(0,strlen($chars) - 1),1);} return $ str}//Get访问令牌公共函数GetAccessToken(){//将json字符串转换成json对象(json_encode就是将数组转换成json字符串,JSON _ decode( ;",true)如果加了true,json字符串就转换成php数组,如果不加true,就转换成PHP对象)$ data = JSON _ DECODE(file _ get _ contents(" access _ token . JSON ;));if($data - gt;expire _ time lttime(){//如果是企业号,使用下面的URL获取access _ token $ URL = " https://api.weixin.qq.com/cgi-bin/token? grant _ type = client _ credential ;appid = $ this - gt;appid secret = $ this - gt;appsecret;$ RES = JSON _ decode($ this - gt;http get($ URL));$ access _ token = $ res- gt;access _ tokenif($ access _ token){ $ data - gt;expire _ time = time()+7000;$data->access _ token = $ access _ token$ FP = fopen( ;access _ token.json , w );fwrite($fp,JSON _ encode($ data));fclose($ FP);} } else { $ access _ token = $ data - gt;access _ token} return $ access _ token}//Get JSAPI _ Ticket(JSAPI _ Ticket是微信官方账号调用微信JS接口使用的临时票证)私有函数GetJSAPI Ticket(){//JSAPI _ Ticket应该全局存储和更新。以下代码作为示例写入文件中$ data = JSON _ decode(file _ get _ contents(" jsapi _ ticket . JSON ;));if($data - gt;expire _ time lttime()){ $ access token = $ this - gt;getAccessToken();//如果是企业号,用下面的URL//$ URL = " https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket? access _ token = $ access token ;;$ url = https://api.weixin.qq.com/cgi-bin/ticket/getticket?类型= jsapi access _ token = $ access token ;;$ RES = JSON _ decode($ this - gt;http get($ URL));$ ticket = $ res- gt;票;if($ ticket){ $ data - gt;expire _ time = time()+7000;$data->jsapi _ ticket = $ ticket$ FP = fopen( ;jsapi _ ticket.json , w );fwrite($fp,JSON _ encode($ data));fclose($ FP);} } else { $ ticket = $ data - gt;jsapi _ ticket} return $ ticket}//获取用户的OpenID公共函数OpenID(){ $ URL = $ _ server[ # 39;HTTP _ HOST # 39] .$ _服务器[ # 39;请求_ URI # 39;];如果(!isset($ _ GET[ # 39;代码 # 39;])){//获取程序集的URL $ OpenID URL = $ this ->;snsapi _ base($ URL);重定向($ openidUrl);} else { $ OpenID access _ token = $ this - gt;OpenID access _ token($ _ GET[ # 39;代码 # 39;]);返回$ openidAccess _ token} }//获取微信用户的OPNID公共函数Get OpenID($OpenID,$access _ token){$userinfo =$this ->;getUserInfo($openid,$ access _ token);返回$ userInfo}公共函数snsapi_base($redirect_uri,$ scope = snsapi _ userinfo ,$ state = 0){ $ appId = $ this - gt;appid$ url = https://open.weixin.qq.com/connect/oauth2/authorize&;;$url。= ?appid = $ appId ;$url。= 重定向uri = http://$ redirect _ uri ;;$url。= response _ type = code ;$url。= scope = $ scope ;$url。= state = $ state #微信_重定向 ;返回$ url}公共函数OpenID access _ token($ code){ $ appId = $ this - gt;appid$ app secret = $ this - gt;appsecret$ url = https://api.weixin.qq.com/sns/oauth2/access_token? appId = $ appId ;secret = $ appSecret code = $ code grant _ type = authorization _ code ;返回JSON _ decode($ this - gt;httpGet($url),true);}//获取用户信息公共函数get userinfo ($ openid,$ access _ token){ $ URL = " https://api.weixin.qq.com/sns/userinfo? access _ token = $ access _ token ;openid = $ openid lang = zh _ CN ;返回JSON _ decode($ this - gt;httpGet($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);返回$ res}}以上是微信调用微信签名包封装的类库细节。请多关注主机参考其他相关文章!
这几篇文章你可能也喜欢:
本文由主机参考刊发,转载请注明:微信调用微信签名包的封装类库 https://zhujicankao.com/81870.html
评论前必须登录!
注册