Common.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Common.php UTF-8
  4. * 公共方法
  5. *
  6. * @date : 2018/9/13 18:18
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huoMpMsg\controller;
  13. use huo\controller\common\Base;
  14. use huolib\constant\CacheConst;
  15. use think\Cache;
  16. use think\Log;
  17. class Common extends Base {
  18. /**
  19. * 获取 access_token
  20. * https://developers.weixin.qq.com/miniprogram/dev/api/token.html
  21. *
  22. * @param string $appid 第三方用户唯一凭证
  23. * @param string $app_secret 第三方用户唯一凭证密钥,即appsecret
  24. * @param bool $read_cache
  25. *
  26. * @return string|bool
  27. */
  28. public static function getAccessToken($appid, $app_secret, $read_cache = true) {
  29. $_cache_key = CacheConst::CACHE_ACCESS_TOKEN_PREFIX.$appid;
  30. $_data = Cache::get($_cache_key);
  31. if (!empty($_data)) {
  32. $_data = json_decode($_data, true);
  33. $_now_time = time();
  34. if (isset($_data['expires_time']) && $_now_time < $_data['expires_time']) {
  35. if ($read_cache) {
  36. return $_data['access_token'];
  37. }
  38. }
  39. }
  40. $_url = 'https://api.weixin.qq.com/cgi-bin/token';
  41. $_param['grant_type'] = 'client_credential';
  42. $_param['appid'] = $appid;
  43. $_param['secret'] = $app_secret;
  44. $_rdata = self::curl($_url, http_build_query($_param));
  45. $_rdata = json_decode($_rdata, true);
  46. if (!isset($_rdata['access_token'])) {
  47. Log::write(
  48. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&appid=$appid&app_secret=$app_secret".
  49. json_encode($_rdata), Log::ERROR
  50. );
  51. return false;
  52. }
  53. $_data['access_token'] = $_rdata['access_token'];
  54. $_data['expires_in'] = $_rdata['expires_in'];
  55. $_data['expires_time'] = time() + $_rdata['expires_in'];
  56. $_rs = Cache::set($_cache_key, json_encode($_data));
  57. if (false == $_rs) {
  58. return false;
  59. }
  60. return $_data['access_token'];
  61. }
  62. /**
  63. * 获取 jsapi_ticket
  64. * https://developers.weixin.qq.com/miniprogram/dev/api/token.html
  65. *
  66. * @param $access_token $appid 第三方用户唯一凭证
  67. *
  68. * @return string|bool
  69. */
  70. public static function getJsapiTicket($access_token) {
  71. $_cache_key = CacheConst::CACHE_JSAPI_TICKET_PREFIX.$access_token;
  72. $_data = Cache::get($_cache_key);
  73. if (!empty($_data)) {
  74. $_data = json_decode($_data, true);
  75. $_now_time = time();
  76. if (isset($_data['expires_time']) && $_now_time < $_data['expires_time']) {
  77. return $_data['ticket'];
  78. }
  79. }
  80. $_url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
  81. $_param['type'] = 'jsapi';
  82. $_param['access_token'] = $access_token;
  83. $_rdata = self::curl($_url, http_build_query($_param));
  84. $_rdata = json_decode($_rdata, true);
  85. if (!isset($_rdata['ticket'])) {
  86. Log::write(
  87. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&access_token=$access_token".
  88. json_encode($_rdata), Log::ERROR
  89. );
  90. return false;
  91. }
  92. $_data['ticket'] = $_rdata['ticket'];
  93. $_data['expires_in'] = $_rdata['expires_in'];
  94. $_data['expires_time'] = time() + $_rdata['expires_in'];
  95. $_rs = Cache::set($_cache_key, json_encode($_data));
  96. if (false == $_rs) {
  97. return false;
  98. }
  99. return $_data['ticket'];
  100. }
  101. /**
  102. * CURL请求
  103. *
  104. * @param string $url 请求的URL
  105. * @param string $params 请求的参数
  106. * @param string $is_get 是否是get请求
  107. * @param array $header 头
  108. *
  109. * @return int|mixed
  110. */
  111. public static function curl($url, $params, $is_get = "GET", $header = []) {
  112. $_header = $header;
  113. $ch = curl_init();
  114. if ("GET" == $is_get) {
  115. if (!empty($params)) {
  116. $url = $url.'?'.$params;
  117. }
  118. curl_setopt($ch, CURLOPT_HEADER, 0); // 过滤HTTP头
  119. } else {
  120. curl_setopt($ch, CURLOPT_POST, 1);
  121. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  122. }
  123. curl_setopt($ch, CURLOPT_URL, $url);
  124. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
  125. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
  126. /* https 请求 */
  127. if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
  128. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  129. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  130. }
  131. if (!empty($_header)) {
  132. array_push($_header, 'Content-Length: '.strlen($params));
  133. curl_setopt($ch, CURLOPT_HTTPHEADER, $_header);
  134. }
  135. $_rs = curl_exec($ch);
  136. if (false === $_rs) {
  137. $_rs = curl_errno($ch);
  138. }
  139. curl_close($ch);
  140. return $_rs;
  141. }
  142. /**
  143. * 判断是否json
  144. *
  145. * @param $string
  146. *
  147. * @return bool
  148. */
  149. public static function isJson($string) {
  150. json_decode($string);
  151. return (json_last_error() == JSON_ERROR_NONE);
  152. }
  153. }