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];
        }

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章