php 推送微信公众号模板消息

2021-09-10  本文已影响0人  风度翩翩的程序猿

自己弄了个类 Wxtui.php

<?php
/**
 * Created by PhpStorm.
 * User: dbag
 */
class WxTui{
    function __construct(){
        
    }
    
   public function cash_message($openid, $data){
        $appid = "********";
        $secret = "*******";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $token = $this->getJson($url);
        $token = json_decode($token, true);
       
        $uri ='https://api.weixin.qq.com/cgi-bin/message/template/send';
        $access_token = $token["access_token"];
        $uri = $uri.'?access_token='.$access_token;
        
        $data=  array('touser'=>$openid,   //发给谁
            'template_id'=>'RKIS1O_9SPSOqp7_R7fCd195p3dTfqMr6pGKNWdbDtU',   //模板id
            'url'=>'#',     //这个是你发送了模板消息之后,当用户点击时跳转的连接
            'topcolor'=>"#FF0000",   //颜色
            'miniprogram' => '',
            'data'=>array(
     
                'first'=>array(
                    'value'=>$data['first'],
                    'color'=>'#173177'
                ),
                'keyword1'=>array(
                    'value'=>$data['course_name'],
                    'color'=>'#173177'
                ),
                'keyword2'=>array(
                    'value'=>$data['teacher_name'],
                    'color'=>'#173177'
                ),
                'keyword3'=>array(
                    'value'=>$data['tel'],
                    'color'=>'#173177'
                ),
                'keyword4'=>array(
                    'value'=>$data['time'],
                    'color'=>'#173177'
                ),
                'remark'=>array(
                    'value'=>$data['remark'],
                    'color'=>'#173177'
                )
            )
        );
        $res_data = $this->getJson($uri,$data);
        $res_data = json_decode($res_data, true);
        if ($res_data['errcode'] != 0) {
            return false;
        }
        return true;
    }
 
  public function getJson($url = '', $param = [] ,$contentType = 'json'){
        $ch = curl_init();
        // 请求地址
        curl_setopt($ch, CURLOPT_URL, $url);
        // 请求参数类型
        $param = $contentType == 'json' ? urldecode(json_encode($param)) : http_build_query($param);
        // 关闭https验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // post提交
        if($param){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }
        // 返回的数据是否自动显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 执行并接收响应结果
        $output = curl_exec($ch);
        // 关闭curl
        curl_close($ch);
        return $output !== false ? $output : false;
     
    }
}

很简单,根据自己的控制器去调用类中的 cash_message(openid ,data)方法 openid就是微信的openid
data是需要推送的信息

$user = $this->db->get_where('t_user',array('id'=>$val['user_id']))->row_array();
$d['first'] = '亲爱的用户,您预约的课程还有'.$config['no_tui'].'小时就开课了,记得准时到达哦!';
 $d['teacher_name'] = $this->db->get_where('t_teacher',array('id'=>$val['teacher_id']))->row_array()['name'];
$d['course_name'] = $this->db->get_where('t_course',array('id'=>$val['course_id']))->row_array()['course_name'];
$d['tel'] = '0510-8270 0058';
$d['time'] = date('Y-m-d H:i:s',$start);
 $d['remark'] = '欢迎您使用SG舞蹈课程预约系统,如有问题咨询:0510-8270 0058';
 // 推送消息
 $msg->cash_message($user['openid'],$d);

这样的话,就推送成功了,记住有一点,推送之前需要在公众号中设置白名单ip要不然获取access_token没有权限!!!!!!!!

上一篇下一篇

猜你喜欢

热点阅读