| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 | 
							- <?php
 
- // 微信 JS 接口签名校验工具: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
 
- //$appid = 'wx8bba7ec467b61efa';
 
- //$secret = 'f934ca9ae9558dec76f38636d737a635';
 
- $appid = 'wx41e478e592f9e04a';
 
- $secret = 'fc165de4aa5cd24b628baa30b100e146';
 
- // 获取token
 
- $token_data = file_get_contents('./wechat_token.txt');
 
- if (!empty($token_data)) {
 
-     $token_data = json_decode($token_data, true);
 
- }
 
- $time  = time() - $token_data['time'];
 
- if ($time > 3600) {
 
-     $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
 
-     $token_res = https_request($token_url);
 
-     $token_res = json_decode($token_res, true);
 
-     $token = $token_res['access_token'];
 
-     $data = array(
 
-         'time' =>time(),
 
-         'token' =>$token
 
-     );
 
-     $res = file_put_contents('./wechat_token.txt', json_encode($data));
 
-     if ($res) {
 
-         echo '更新 token 成功';
 
-     }
 
- } else {
 
-     $token = $token_data['token'];
 
- }
 
- // 获取ticket
 
- $ticket_data = file_get_contents('./wechat_ticket.txt');
 
- if (!empty($ticket_data)) {
 
-     $ticket_data = json_decode($ticket_data, true);
 
- }
 
- $time  = time() - $ticket_data['time'];
 
- if ($time > 3600) {
 
-     $ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi";
 
-     $ticket_res = https_request($ticket_url);
 
-     $ticket_res = json_decode($ticket_res, true);
 
-     $ticket = $ticket_res['ticket'];
 
-     $data = array(
 
-         'time'    =>time(),
 
-         'ticket'  =>$ticket
 
-     );
 
-     $res = file_put_contents('./wechat_ticket.txt', json_encode($data));
 
-     if ($res) {
 
-         echo '更新 ticket 成功';
 
-     }
 
- } else {
 
-     $ticket = $ticket_data['ticket'];
 
- }
 
- // 进行sha1签名
 
- $timestamp = time();
 
- $nonceStr = createNonceStr();
 
- // 注意 URL 建议动态获取(也可以写死).
 
- $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
 
- // $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // 调用JSSDK的页面地址
 
- // $url = $_SERVER['HTTP_REFERER']; // 前后端分离的, 获取请求地址(此值不准确时可以通过其他方式解决)
 
- // $url = "https://s.yxin.tech/hello.html";
 
- $url = $_GET['url'];
 
- $str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}";
 
-     file_put_contents('./sign_before.txt', $str);
 
- $sha_str = sha1($str);
 
-     file_put_contents('./sign_after.txt', $sha_str);
 
- function createNonceStr($length = 16) {
 
-     $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
-     $str = "";
 
-     for ($i = 0; $i < $length; $i++) {
 
-       $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
 
-     }
 
-     return $str;
 
- }
 
- /**
 
-  * 模拟 http 请求
 
-  * @param  String $url  请求网址
 
-  * @param  Array  $data 数据
 
-  */
 
- function https_request($url, $data = null){
 
-     // curl 初始化
 
-     $curl = curl_init();
 
-     // curl 设置
 
-     curl_setopt($curl, CURLOPT_URL, $url);
 
-     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 
-     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
 
-     // 判断 $data get  or post
 
-     if ( !empty($data) ) {
 
-         curl_setopt($curl, CURLOPT_POST, 1);
 
-         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 
-     }
 
-     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
-     // 执行
 
-     $res = curl_exec($curl);
 
-     curl_close($curl);
 
-     return $res;
 
- }
 
- echo json_encode([
 
-     'timestamp'=>$timestamp,
 
-         'nonceStr'=>$nonceStr,
 
-     'appid'=>$appid,
 
-     'sha_str'=>$sha_str,
 
-     
 
-     ]);
 
- ?>
 
 
  |