推送模板类
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);
总结:根据自己实际模板调整模板类的发送属性
期待大家点赞支持