sign.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // 微信 JS 接口签名校验工具: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
  3. //$appid = 'wx8bba7ec467b61efa';
  4. //$secret = 'f934ca9ae9558dec76f38636d737a635';
  5. $appid = 'wx4eab2e3b5531d58b';
  6. $secret = '1026580a52dc89f564395113017c165b';
  7. // 获取token
  8. $token_data = file_get_contents('./wechat_token.txt');
  9. if (!empty($token_data)) {
  10. $token_data = json_decode($token_data, true);
  11. }
  12. $time = time() - $token_data['time'];
  13. if ($time > 3600) {
  14. $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
  15. $token_res = https_request($token_url);
  16. $token_res = json_decode($token_res, true);
  17. $token = $token_res['access_token'];
  18. $data = array(
  19. 'time' =>time(),
  20. 'token' =>$token
  21. );
  22. $res = file_put_contents('./wechat_token.txt', json_encode($data));
  23. if ($res) {
  24. echo '更新 token 成功';
  25. }
  26. } else {
  27. $token = $token_data['token'];
  28. }
  29. // 获取ticket
  30. $ticket_data = file_get_contents('./wechat_ticket.txt');
  31. if (!empty($ticket_data)) {
  32. $ticket_data = json_decode($ticket_data, true);
  33. }
  34. $time = time() - $ticket_data['time'];
  35. if ($time > 3600) {
  36. $ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi";
  37. $ticket_res = https_request($ticket_url);
  38. $ticket_res = json_decode($ticket_res, true);
  39. $ticket = $ticket_res['ticket'];
  40. $data = array(
  41. 'time' =>time(),
  42. 'ticket' =>$ticket
  43. );
  44. $res = file_put_contents('./wechat_ticket.txt', json_encode($data));
  45. if ($res) {
  46. echo '更新 ticket 成功';
  47. }
  48. } else {
  49. $ticket = $ticket_data['ticket'];
  50. }
  51. // 进行sha1签名
  52. $timestamp = time();
  53. $nonceStr = createNonceStr();
  54. // 注意 URL 建议动态获取(也可以写死).
  55. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  56. // $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // 调用JSSDK的页面地址
  57. // $url = $_SERVER['HTTP_REFERER']; // 前后端分离的, 获取请求地址(此值不准确时可以通过其他方式解决)
  58. // $url = "https://s.yxin.tech/hello.html";
  59. $url = $_GET['url'];
  60. $str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";
  61. file_put_contents('./sign_before.txt', $str);
  62. $sha_str = sha1($str);
  63. file_put_contents('./sign_after.txt', $sha_str);
  64. function createNonceStr($length = 16) {
  65. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  66. $str = "";
  67. for ($i = 0; $i < $length; $i++) {
  68. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  69. }
  70. return $str;
  71. }
  72. /**
  73. * 模拟 http 请求
  74. * @param String $url 请求网址
  75. * @param Array $data 数据
  76. */
  77. function https_request($url, $data = null){
  78. // curl 初始化
  79. $curl = curl_init();
  80. // curl 设置
  81. curl_setopt($curl, CURLOPT_URL, $url);
  82. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  83. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  84. // 判断 $data get or post
  85. if ( !empty($data) ) {
  86. curl_setopt($curl, CURLOPT_POST, 1);
  87. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  88. }
  89. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  90. // 执行
  91. $res = curl_exec($curl);
  92. curl_close($curl);
  93. return $res;
  94. }
  95. echo json_encode([
  96. 'timestamp'=>$timestamp,
  97. 'nonceStr'=>$nonceStr,
  98. 'appid'=>$appid,
  99. 'sha_str'=>$sha_str,
  100. ]);
  101. ?>