123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace Jiaruan;
- use think\facade\Cache;
- use think\facade\Env;
- class WxTmp {
-
- public function getAccessToken( $appid = '', $appsecret = ''){
- $appid = $appid ?: C('WECHAT_APPID_3');
- $appsecret = $appsecret ?: C('WECHAT_APPSECRET_3');
- //从redis获取token
- $redis=Cache::store('redis')->handler();
-
- // $p = Redis("get_access_token","hash");
- // $val = $p->get($appid);
- $val = $redis->hget('get_access_token', $appid);
- $info = json_decode($val,true);
- if($info['access_token']&&time()<$info['expirtime']){
-
- $access_token = $info['access_token'];
-
- }else{
- $ur="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
- $chu= curl_init();
- curl_setopt($chu, CURLOPT_URL, $ur);
- curl_setopt($chu, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($chu, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($chu, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($chu);
- curl_close($chu);
- $jsoninfo = json_decode($output, true);
- $access_token = $jsoninfo["access_token"];
- if(!$access_token){
- echo 'access_token is empty!'.PHP_EOL;
- return false;
- }
- //将access_token存到redis
- $data = array(
- 'expirtime'=>time()+5400,
- 'access_token'=>$access_token
- );
-
- $key = $appid;
- $val = json_encode($data,JSON_UNESCAPED_UNICODE);
- try{
- $redis->hset('get_access_token', $appid, $val);
- }catch(\Exception $e){
- return false;
- }
-
-
- }
-
- /*获取access_token*/
- return $access_token;
- }
-
-
- public function sendMessage( $msginfo, $config = array() ){
- if(!$msginfo){
- $msg = 'template message is empty!'.PHP_EOL;
- return array('success'=>false, 'message' =>$msg );
- }
- if( empty($config) || !isset($config['appid']) || !isset($config['appsecret']) ){ // 兼容之前固定使用这个常量的
- $config['appid'] = Env::get('wechat.wechat_appid');
- $config['appsecret'] =Env::get('wechat.wechat_appsecret');
- }
- //获取access_token
-
- $access_token = $this->getAccessToken($config['appid'], $config['appsecret']);
-
- if(!$access_token){
- $msg = 'when you sendMessage,the access_token is null!'.PHP_EOL;
- return array('success'=>false, 'message' =>$msg );
- }
- $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token ;
- /*
- $arr = array(
- 'touser'=>$msginfo['openid'],
- 'template_id'=>"MC5f9ikF-UWc42bc-B3uJuBcn-qFEBOmE5Fw4lK9bG0",
- "topcolor"=>"#FF0000",
- 'url'=>"http://www.baidu.com",
- 'data'=>array(
- 'name'=>array('value'=>'Lucy','color'=>"#173177") ,
- 'time'=>array('value'=>date('Y-m-d'),'color'=>"#173177") ,
- 'location'=>array('value'=>'bj','color'=>"#173177")
- )
-
- );*/
-
- //汉字不转义
- $postJson = json_encode($msginfo,JSON_UNESCAPED_UNICODE);
- if(!$postJson){
- $msg = 'data json_encode faild';
- return array('success'=>false, 'message' =>$msg );
- }
- //cURL post请求
- $res = $this->curlPost($url,$postJson);
- $res = json_decode($res,true);
-
- if(!$res['errcode']){
- return array('success'=>true, 'message' => $res['errmsg']);
- }else{
- return array('success'=>false, 'message' => $res['errmsg']);
- }
- }
-
-
- public function curlPost( $url, $postJson ){
- if(!$url){
- echo 'url is null!'.PHP_EOL;
- return false;
- }
- if(!$postJson){
- echo 'postJson is null!'.PHP_EOL;
- return false;
- }
- $ch = curl_init();
- $params[CURLOPT_URL] = $url; //请求url地址
- $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
- $params[CURLOPT_SSL_VERIFYPEER] = false;
- $params[CURLOPT_SSL_VERIFYHOST] = false;
- $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
- $params[CURLOPT_POST] = true;
- $params[CURLOPT_POSTFIELDS] = $postJson;
- curl_setopt_array($ch, $params); //传入curl参数
- $content = curl_exec($ch); //执行
- curl_close($ch); //关闭连接
-
- return $content;
-
- }
-
-
- public function getTemplateList( $config ){
- if( empty($config) || !isset($config['appid']) || !isset($config['appsecret']) ){ // 兼容之前固定使用这个常量的
- $msg = 'appid and appsecret not found';
- return array('success'=>false, 'message' => $msg);
- }
- //获取access_token
- $access_token = $this->getAccessToken($config['appid'], $config['appsecret']);
- if(!$access_token){
- $msg = 'when you sendMessage,the access_token is null!'.PHP_EOL;
- return array('success'=>false, 'message' =>$msg );
- }
- $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=".$access_token ;
-
- //汉字不转义
- /*$postJson = json_encode($msginfo,JSON_UNESCAPED_UNICODE);
- if(!$postJson){
- $msg = 'data json_encode faild';
- return array('success'=>false, 'message' =>$msg );
- }*/
- //cURL post请求
- $res = $this->curlGetTemplate($url);
- $res = json_decode($res,true);
- return $res;
- }
-
-
- public function curlGetTemplate( $url ){
- if(!$url){
- echo 'url is null!'.PHP_EOL;
- return false;
- }
- $ch = curl_init();
- $params[CURLOPT_URL] = $url; //请求url地址
- $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
- $params[CURLOPT_SSL_VERIFYPEER] = false;
- $params[CURLOPT_SSL_VERIFYHOST] = false;
- $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
- //$params[CURLOPT_POST] = true;
- //$params[CURLOPT_POSTFIELDS] = $postJson;
- curl_setopt_array($ch, $params); //传入curl参数
- $content = curl_exec($ch); //执行
- curl_close($ch); //关闭连接
-
- return $content;
- }
-
-
-
- }
|