jssdk.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. $protocol = !empty($_SERVER[HTTPS]) && $_SERVER[HTTPS] !== off || $_SERVER[SERVER_PORT] == 443 ? 'https://' : 'http://';
  15. $url = $protocol . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
  16. $timestamp = time();
  17. $nonceStr = $this->createNonceStr();
  18. $string = 'jsapi_ticket=' . $jsapiTicket . '&noncestr=' . $nonceStr . '&timestamp=' . $timestamp . '&url=' . $url;
  19. $signature = sha1($string);
  20. $signPackage = array('appId' => $this->appId, 'nonceStr' => $nonceStr, 'timestamp' => $timestamp, 'url' => $url, 'signature' => $signature, 'rawString' => $string);
  21. return $signPackage;
  22. }
  23. private function createNonceStr($length = 16)
  24. {
  25. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  26. $str = '';
  27. for ($i = 0; $i < $length; $i++) {
  28. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  29. }
  30. return $str;
  31. }
  32. private function getJsApiTicket()
  33. {
  34. $data = json_decode($this->get_php_file(MYMPS_DATA . '/jsapi/jsapi_ticket.php'));
  35. if ($data->expire_time < time()) {
  36. $accessToken = $this->getAccessToken();
  37. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=' . $accessToken;
  38. $res = json_decode($this->httpGet($url));
  39. $ticket = $res->ticket;
  40. if ($ticket) {
  41. $data->expire_time = time() + 7000;
  42. $data->jsapi_ticket = $ticket;
  43. $this->set_php_file(MYMPS_DATA . '/jsapi/jsapi_ticket.php', json_encode($data));
  44. }
  45. }
  46. else {
  47. $ticket = $data->jsapi_ticket;
  48. }
  49. return $ticket;
  50. }
  51. private function getAccessToken()
  52. {
  53. $data = json_decode($this->get_php_file(MYMPS_DATA . '/jsapi/jsapi_access_token.php'));
  54. if ($data->expire_time < time()) {
  55. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret;
  56. $res = json_decode($this->httpGet($url));
  57. $access_token = $res->access_token;
  58. if ($access_token) {
  59. $data->expire_time = time() + 7000;
  60. $data->access_token = $access_token;
  61. $this->set_php_file(MYMPS_DATA . '/jsapi/jsapi_access_token.php', json_encode($data));
  62. }
  63. }
  64. else {
  65. $access_token = $data->access_token;
  66. }
  67. return $access_token;
  68. }
  69. private function httpGet($url)
  70. {
  71. $curl = curl_init();
  72. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  73. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  74. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  75. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  76. curl_setopt($curl, CURLOPT_URL, $url);
  77. $res = curl_exec($curl);
  78. curl_close($curl);
  79. return $res;
  80. }
  81. private function get_php_file($filename)
  82. {
  83. return trim(substr(file_get_contents($filename), 15));
  84. }
  85. private function set_php_file($filename, $content)
  86. {
  87. $fp = fopen($filename, 'w');
  88. fwrite($fp, '<?php exit();?>' . $content);
  89. fclose($fp);
  90. }
  91. }
  92. ?>