You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. class JSSDK
  3. {
  4. private $appId;
  5. private $appSecret;
  6. public function __construct($appId, $appSecret)
  7. {
  8. $this->appId = $appId;
  9. $this->appSecret = $appSecret;
  10. }
  11. public function getSignPackage()
  12. {
  13. $jsapiTicket = $this->getJsApiTicket();
  14. // 注意 URL 一定要动态获取,不能 hardcode.
  15. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  16. $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  17. $timestamp = time();
  18. $nonceStr = $this->createNonceStr();
  19. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  20. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  21. $signature = sha1($string);
  22. $signPackage = array(
  23. "appId" => $this->appId,
  24. "nonceStr" => $nonceStr,
  25. "timestamp" => $timestamp,
  26. "url" => $url,
  27. "signature" => $signature,
  28. "rawString" => $string
  29. );
  30. return $signPackage;
  31. }
  32. private function createNonceStr($length = 16)
  33. {
  34. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  35. $str = "";
  36. for ($i = 0; $i < $length; $i++)
  37. {
  38. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  39. }
  40. return $str;
  41. }
  42. private function getJsApiTicket()
  43. {
  44. // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
  45. $data = json_decode($this->get_php_file("jsapi_ticket.php"));
  46. if ($data->expire_time < time())
  47. {
  48. $accessToken = $this->getAccessToken();
  49. // 如果是企业号用以下 URL 获取 ticket
  50. // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
  51. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
  52. $res = json_decode($this->httpGet($url));
  53. $ticket = $res->ticket;
  54. if ($ticket)
  55. {
  56. $data->expire_time = time() + 7000;
  57. $data->jsapi_ticket = $ticket;
  58. $this->set_php_file("jsapi_ticket.php", json_encode($data));
  59. }
  60. }
  61. else
  62. {
  63. $ticket = $data->jsapi_ticket;
  64. }
  65. return $ticket;
  66. }
  67. private function getAccessToken()
  68. {
  69. // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
  70. $data = json_decode($this->get_php_file("access_token.php"));
  71. if ($data->expire_time < time())
  72. {
  73. // 如果是企业号用以下URL获取access_token
  74. // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
  75. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
  76. $res = json_decode($this->httpGet($url));
  77. $access_token = $res->access_token;
  78. if ($access_token)
  79. {
  80. $data->expire_time = time() + 7000;
  81. $data->access_token = $access_token;
  82. $this->set_php_file("access_token.php", json_encode($data));
  83. }
  84. }
  85. else
  86. {
  87. $access_token = $data->access_token;
  88. }
  89. return $access_token;
  90. }
  91. private function httpGet($url)
  92. {
  93. $curl = curl_init();
  94. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  95. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  96. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  97. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  98. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  99. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  100. curl_setopt($curl, CURLOPT_URL, $url);
  101. $res = curl_exec($curl);
  102. curl_close($curl);
  103. return $res;
  104. }
  105. private function get_php_file($filename)
  106. {
  107. //return trim(substr(file_get_contents($filename), 15)); //原版
  108. return trim(substr(file_get_contents(resource_path('org/wxJsSdk/'.$filename)), 15)); //Laravel版
  109. }
  110. private function set_php_file($filename, $content)
  111. {
  112. //$fp = fopen($filename, "w"); //原版
  113. $fp = fopen(resource_path('org/wxJsSdk/'.$filename), "w"); //Laravel版
  114. fwrite($fp, "<?php exit();?>" . $content);
  115. fclose($fp);
  116. }
  117. }