微信推送模板消息

7 篇文章 0 订阅

推送模板类


  function order_push($openid,$orderType, $customerInfo,$orderItemData, $remark)
    {
        $template_id = '模板id';//可以在微信公众号申请获取
        $urls = "请求地址";
        $appid = 'APPId';
        $secret = 'secretId';
        $access_token = $this->getToken($appid, $secret);
        if (!$access_token || empty($access_token)) {
            $arrContextOptions = array(
                "ssl" => array(
                    "verify_peer" => false,
                    "verify_peer_name" => false,
                ),
            );
//获取ACCESS_TOKEN
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
            if (function_exists('file_get_contents')) {
                $access_token = file_get_contents($url, false, stream_context_create($arrContextOptions));
            } else {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $access_token = curl_exec($ch);
                curl_close($ch);
            }
            $access = json_decode($access_token, true);
            $access_token = $access['access_token'];
            Cache::set('ACCESS_TOKEN', $access_token, '7000');
        }

        $sendurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
        $data = [];
        $data['touser'] = $openid;
        $data['template_id'] = $template_id;
        $data['url'] = $urls;
        $first = "您有订单来了,请注意查看!!!";
        $tradeDateTime = date('Y-m-d H:i:s', time());
        $orderItemName = '订单号';
        $data['data']['first'] = array('value' => $first, 'color' => '#173177');
        $data['data']['tradeDateTime'] = array('value' => $tradeDateTime, 'color' => '#173177');
        $data['data']['orderType'] = array('value' => $orderType, 'color' => '#173177');
        $data['data']['customerInfo'] = array('value' => $customerInfo, 'color' => '#173177');
        $data['data']['orderItemName'] = array('value' => $orderItemName, 'color' => '#173177');
        $data['data']['orderItemData'] = array('value' => $orderItemData, 'color' => '#173177');
        $data['data']['remark'] = array('value' => $remark, 'color' => '#173177');

//        print_r($data);exit;
        $datajson = json_encode($data);
//        print_r($datajson);exit;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $sendurl);//要访问的地址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回,0是返回,1是不返回
        curl_setopt($ch, CURLOPT_POST, 1);// 发送一个常规的POST请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $datajson);
        $output = curl_exec($ch);//执行并获取数据
        curl_close($ch);
        $output = json_decode($output);
//存入模板发送日志
        $logs = "发送用户openid:" . $openid . ",模板id:" . $template_id . ",跳转链接:" . $urls . ",参数first:" . $first . ",tradeDateTime:" . $tradeDateTime . ",orderTyp:" . $orderType . ",customerInfo:" . $customerInfo . ",orderItemName:" . $orderItemName . ",orderItemData:" . $orderItemData . ",remark:" . $remark;
        $logs .= "返回参数:errcode:" . $output->errcode . ",errmsg:" . $output->errmsg;
        file_put_contents(LOG_PATH . date("Ymd", time()) . "model.txt", $logs);
        if ($output->errcode == '40001') {
            Cache::set('ACCESS_TOKEN', '');
            $this->wxsend($openid, $template_id, $urls, $first, $tradeDateTime, $orderType, $customerInfo, $orderItemName, $remark);
        }
//        dump($output);
    }

    //获取微信token
     function getToken($appid, $appsecret)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
        $token = $this->request_get($url);
        $token = json_decode(stripslashes($token));
        $arr = json_decode(json_encode($token), true);
        $access_token = $arr['access_token'];
        return $access_token;
    }

     function request_get($url = '')
    {
        if (empty($url)) {
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

推送事件

$openid = '推送用户openid';
		$customerInfo = '推送用户名称';
		$remark = '备注';
		$orderType =  '推送类型';
		$orderItemName = '信息';
		$orderItemData = '推送用户订单号';
		$tradeDateTime = date('Y-m-d H:i:s',time());
		$this->order_push($openid,$tradeDateTime,$orderType,$customerInfo,$orderItemName,$orderItemData,$remark);

总结:根据自己实际模板调整模板类的发送属性

期待大家点赞支持 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杰姆小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值