WxTmp.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Jiaruan;
  3. use think\facade\Cache;
  4. use think\facade\Env;
  5. class WxTmp {
  6. public function getAccessToken( $appid = '', $appsecret = ''){
  7. $appid = $appid ?: C('WECHAT_APPID_3');
  8. $appsecret = $appsecret ?: C('WECHAT_APPSECRET_3');
  9. //从redis获取token
  10. $redis=Cache::store('redis')->handler();
  11. // $p = Redis("get_access_token","hash");
  12. // $val = $p->get($appid);
  13. $val = $redis->hget('get_access_token', $appid);
  14. $info = json_decode($val,true);
  15. if($info['access_token']&&time()<$info['expirtime']){
  16. $access_token = $info['access_token'];
  17. }else{
  18. $ur="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
  19. $chu= curl_init();
  20. curl_setopt($chu, CURLOPT_URL, $ur);
  21. curl_setopt($chu, CURLOPT_SSL_VERIFYPEER, FALSE);
  22. curl_setopt($chu, CURLOPT_SSL_VERIFYHOST, FALSE);
  23. curl_setopt($chu, CURLOPT_RETURNTRANSFER, 1);
  24. $output = curl_exec($chu);
  25. curl_close($chu);
  26. $jsoninfo = json_decode($output, true);
  27. $access_token = $jsoninfo["access_token"];
  28. if(!$access_token){
  29. echo 'access_token is empty!'.PHP_EOL;
  30. return false;
  31. }
  32. //将access_token存到redis
  33. $data = array(
  34. 'expirtime'=>time()+5400,
  35. 'access_token'=>$access_token
  36. );
  37. $key = $appid;
  38. $val = json_encode($data,JSON_UNESCAPED_UNICODE);
  39. try{
  40. $redis->hset('get_access_token', $appid, $val);
  41. }catch(\Exception $e){
  42. return false;
  43. }
  44. }
  45. /*获取access_token*/
  46. return $access_token;
  47. }
  48. public function sendMessage( $msginfo, $config = array() ){
  49. if(!$msginfo){
  50. $msg = 'template message is empty!'.PHP_EOL;
  51. return array('success'=>false, 'message' =>$msg );
  52. }
  53. if( empty($config) || !isset($config['appid']) || !isset($config['appsecret']) ){ // 兼容之前固定使用这个常量的
  54. $config['appid'] = Env::get('wechat.wechat_appid');
  55. $config['appsecret'] =Env::get('wechat.wechat_appsecret');
  56. }
  57. //获取access_token
  58. $access_token = $this->getAccessToken($config['appid'], $config['appsecret']);
  59. if(!$access_token){
  60. $msg = 'when you sendMessage,the access_token is null!'.PHP_EOL;
  61. return array('success'=>false, 'message' =>$msg );
  62. }
  63. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token ;
  64. /*
  65. $arr = array(
  66. 'touser'=>$msginfo['openid'],
  67. 'template_id'=>"MC5f9ikF-UWc42bc-B3uJuBcn-qFEBOmE5Fw4lK9bG0",
  68. "topcolor"=>"#FF0000",
  69. 'url'=>"http://www.baidu.com",
  70. 'data'=>array(
  71. 'name'=>array('value'=>'Lucy','color'=>"#173177") ,
  72. 'time'=>array('value'=>date('Y-m-d'),'color'=>"#173177") ,
  73. 'location'=>array('value'=>'bj','color'=>"#173177")
  74. )
  75. );*/
  76. //汉字不转义
  77. $postJson = json_encode($msginfo,JSON_UNESCAPED_UNICODE);
  78. if(!$postJson){
  79. $msg = 'data json_encode faild';
  80. return array('success'=>false, 'message' =>$msg );
  81. }
  82. //cURL post请求
  83. $res = $this->curlPost($url,$postJson);
  84. $res = json_decode($res,true);
  85. if(!$res['errcode']){
  86. return array('success'=>true, 'message' => $res['errmsg']);
  87. }else{
  88. return array('success'=>false, 'message' => $res['errmsg']);
  89. }
  90. }
  91. public function curlPost( $url, $postJson ){
  92. if(!$url){
  93. echo 'url is null!'.PHP_EOL;
  94. return false;
  95. }
  96. if(!$postJson){
  97. echo 'postJson is null!'.PHP_EOL;
  98. return false;
  99. }
  100. $ch = curl_init();
  101. $params[CURLOPT_URL] = $url; //请求url地址
  102. $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
  103. $params[CURLOPT_SSL_VERIFYPEER] = false;
  104. $params[CURLOPT_SSL_VERIFYHOST] = false;
  105. $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
  106. $params[CURLOPT_POST] = true;
  107. $params[CURLOPT_POSTFIELDS] = $postJson;
  108. curl_setopt_array($ch, $params); //传入curl参数
  109. $content = curl_exec($ch); //执行
  110. curl_close($ch); //关闭连接
  111. return $content;
  112. }
  113. public function getTemplateList( $config ){
  114. if( empty($config) || !isset($config['appid']) || !isset($config['appsecret']) ){ // 兼容之前固定使用这个常量的
  115. $msg = 'appid and appsecret not found';
  116. return array('success'=>false, 'message' => $msg);
  117. }
  118. //获取access_token
  119. $access_token = $this->getAccessToken($config['appid'], $config['appsecret']);
  120. if(!$access_token){
  121. $msg = 'when you sendMessage,the access_token is null!'.PHP_EOL;
  122. return array('success'=>false, 'message' =>$msg );
  123. }
  124. $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=".$access_token ;
  125. //汉字不转义
  126. /*$postJson = json_encode($msginfo,JSON_UNESCAPED_UNICODE);
  127. if(!$postJson){
  128. $msg = 'data json_encode faild';
  129. return array('success'=>false, 'message' =>$msg );
  130. }*/
  131. //cURL post请求
  132. $res = $this->curlGetTemplate($url);
  133. $res = json_decode($res,true);
  134. return $res;
  135. }
  136. public function curlGetTemplate( $url ){
  137. if(!$url){
  138. echo 'url is null!'.PHP_EOL;
  139. return false;
  140. }
  141. $ch = curl_init();
  142. $params[CURLOPT_URL] = $url; //请求url地址
  143. $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
  144. $params[CURLOPT_SSL_VERIFYPEER] = false;
  145. $params[CURLOPT_SSL_VERIFYHOST] = false;
  146. $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
  147. //$params[CURLOPT_POST] = true;
  148. //$params[CURLOPT_POSTFIELDS] = $postJson;
  149. curl_setopt_array($ch, $params); //传入curl参数
  150. $content = curl_exec($ch); //执行
  151. curl_close($ch); //关闭连接
  152. return $content;
  153. }
  154. }