WxBase.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\common\library\wechat;
  3. use think\Cache;
  4. use app\common\library\helper;
  5. use app\common\exception\BaseException;
  6. /**
  7. * 微信api基类
  8. * Class wechat
  9. * @package app\library
  10. */
  11. class WxBase
  12. {
  13. protected $appId;
  14. protected $appSecret;
  15. protected $error;
  16. /**
  17. * 构造函数
  18. * WxBase constructor.
  19. * @param $appId
  20. * @param $appSecret
  21. */
  22. public function __construct($appId = null, $appSecret = null)
  23. {
  24. $this->setConfig($appId, $appSecret);
  25. }
  26. protected function setConfig($appId = null, $appSecret = null)
  27. {
  28. !empty($appId) && $this->appId = $appId;
  29. !empty($appSecret) && $this->appSecret = $appSecret;
  30. }
  31. /**
  32. * 写入日志记录
  33. * @param $values
  34. * @return bool|int
  35. */
  36. protected function doLogs($values)
  37. {
  38. return log_write($values);
  39. }
  40. /**
  41. * 获取access_token
  42. * @return mixed
  43. * @throws BaseException
  44. */
  45. protected function getAccessToken()
  46. {
  47. $cacheKey = $this->appId . '@access_token';
  48. if (!Cache::get($cacheKey)) {
  49. // 请求API获取 access_token
  50. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
  51. $result = $this->get($url);
  52. $response = $this->jsonDecode($result);
  53. if (array_key_exists('errcode', $response)) {
  54. throw new BaseException(['msg' => "access_token获取失败,错误信息:{$result}"]);
  55. }
  56. // 记录日志
  57. $this->doLogs([
  58. 'describe' => '获取access_token',
  59. 'url' => $url,
  60. 'appId' => $this->appId,
  61. 'result' => $result
  62. ]);
  63. // 写入缓存
  64. Cache::set($cacheKey, $response['access_token'], 6000); // 7000
  65. }
  66. return Cache::get($cacheKey);
  67. }
  68. /**
  69. * 模拟GET请求 HTTPS的页面
  70. * @param string $url 请求地址
  71. * @return string $result
  72. */
  73. protected function get($url)
  74. {
  75. $curl = curl_init();
  76. curl_setopt($curl, CURLOPT_URL, $url);
  77. curl_setopt($curl, CURLOPT_HEADER, 0);
  78. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  79. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
  80. $result = curl_exec($curl);
  81. curl_close($curl);
  82. return $result;
  83. }
  84. /**
  85. * 模拟POST请求
  86. * @param $url
  87. * @param array $data
  88. * @param bool $useCert
  89. * @param array $sslCert
  90. * @return mixed
  91. */
  92. protected function post($url, $data = [], $useCert = false, $sslCert = [])
  93. {
  94. $header = [
  95. 'Content-type: application/json;'
  96. ];
  97. $curl = curl_init();
  98. curl_setopt($curl, CURLOPT_URL, $url);
  99. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  100. curl_setopt($curl, CURLOPT_HEADER, false);
  101. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  102. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  103. curl_setopt($curl, CURLOPT_POST, TRUE);
  104. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  105. if ($useCert == true) {
  106. // 设置证书:cert 与 key 分别属于两个.pem文件
  107. curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
  108. curl_setopt($curl, CURLOPT_SSLCERT, $sslCert['certPem']);
  109. curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
  110. curl_setopt($curl, CURLOPT_SSLKEY, $sslCert['keyPem']);
  111. }
  112. $result = curl_exec($curl);
  113. curl_close($curl);
  114. return $result;
  115. }
  116. /**
  117. * 模拟POST请求 [第二种方式, 用于兼容微信api]
  118. * @param $url
  119. * @param array $data
  120. * @return mixed
  121. */
  122. protected function post2($url, $data = [])
  123. {
  124. $header = [
  125. 'Content-Type: application/x-www-form-urlencoded'
  126. ];
  127. $ch = curl_init();
  128. curl_setopt($ch, CURLOPT_URL, $url);
  129. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  130. curl_setopt($ch, CURLOPT_POST, true);
  131. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  132. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
  134. $result = curl_exec($ch);
  135. curl_close($ch);
  136. return $result;
  137. }
  138. /**
  139. * 数组转json
  140. * @param $data
  141. * @return string
  142. */
  143. protected function jsonEncode($data)
  144. {
  145. return helper::jsonEncode($data);
  146. }
  147. /**
  148. * json转数组
  149. * @param $json
  150. * @return mixed
  151. */
  152. protected function jsonDecode($json)
  153. {
  154. return helper::jsonDecode($json);
  155. }
  156. /**
  157. * 返回错误信息
  158. * @return mixed
  159. */
  160. public function getError()
  161. {
  162. return $this->error;
  163. }
  164. }