php 发送微信模板消息

        $openid = Db::name('weixin_user')->where('ecuid',$uid)->value('fake_id');
        $data=[
        'touser'=>$openid,
        'template_id'=>'yAscWrOLQtLuSDEPxq6GClxRFJ0xSSHyUqBpmgPCsJs',
        'url'=>$web_url = "http://".$_SERVER['SERVER_NAME'],
        'topcolor'=>"#FF0000",
        'data'=>array(
            'first'=>array('value'=>"您好,有新业务请您尽快确认处理。"),
            'keyword1'=>array('value'=>$xiangmu_id . '--' . $xiangmu . '--' . $jiedian), 
            'keyword2'=>array('value'=>'待确认'),
            'remark'=>array('value'=>"确认完成后,请及时提交!",'color'=>"#173177"),
            )
        ];    
        $tes = $this->sendtempletsmsg($data);
    public function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    public function get_all_access_token(){
        
        $access_token_jilu = Db::name('weixin_config')->where('id',1)->find();
        if(time()-intval($access_token_jilu['token_exp'])>5000){
            $appid = $access_token_jilu['appid'];
            $secret =  $access_token_jilu['appsecret'];
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
            $res = $this->http_curl($url);
            // print_r($res);exit;
            $access_token = $res['access_token'];
            
            $update_data = [
            'token_exp' =>time(),
            'access_token'=>$access_token
            ];
            $update_data = Db::name('weixin_config')->where('id',1)->update($update_data);
        }else{
            $access_token = $access_token_jilu['access_token'];
            
        }
        return $access_token;

    }
    public  function http_curl($url,$type='get',$res='json',$arr=''){
        /*
        $url 请求的url
        $type 请求类型
        $res 返回数据类型
        $arr post请求参数
         */
        $ch=curl_init();
        /*$url='https://api.weixin.qq.com/cgi-bin/token?

        grant_type=client_credential&appid=wxf71d53c65df41aab&secret=e31e44c35067fb75759f53eed1cb1b26';  */
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if($type=='post'){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        if($res=='json'){
            return json_decode($output,true);
        }
    }
    public  function sendtempletsmsg($data){

        $get_all_access_token = $this->get_all_access_token();

        // print_r($get_all_access_token);exit;
        
        $json_data=json_encode($data);//转化成json数组让微信可以接收

        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$get_all_access_token;//模板消息请求URL
        
        $res=$this->https_request($url,urldecode($json_data));//请求开始

        $res=json_decode($res,true);

        if($res['errcode']==0 && $res['errcode']=="ok"){            
            return ['code'=>1];
        }else{
            return ['code'=>-4];
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值