public function wxsend($openid,$template_id,$urls,$first,$keyword1,$keyword2,$keyword3,$remark)
{
$access_token = Cache::get('ACCESS_TOKEN');
if (!$access_token||empty($access_token)){
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$appid = Config::get('APPID');
$secret = Config::get('SECRET');
//获取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;
// if ($urls != ''){
$data['url'] = $urls;
// }
// $data['miniprogram']['appid'] = '';
// $data['miniprogram']['pagepath'] = '';
$data['data']['first'] = array('value'=>$first,'color'=>'#173177');
$data['data']['keyword1'] = array('value'=>$keyword1,'color'=>'#173177');
$data['data']['keyword2'] = array('value'=>$keyword2,'color'=>'#173177');
if ($keyword3 != ''){
$data['data']['keyword3'] = array('value'=>$keyword3,'color'=>'#173177');
}
$data['data']['remark'] = array('value'=>$remark,'color'=>'#173177');
$datajson = json_encode($data);
$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.",keywrod1:".$keyword1.",keyword2:".$keyword2.",keyword3:".$keyword3.",remark:".$remark;
$logs.="返回参数:errcode:".$output->errcode.",errmsg:".$output->errmsg;
file_put_contents(LOG_PATH."wxmodel/" . date("YmdHis", time()) . rand(11111,99999) . "model.txt", $logs);
if ($output->errcode == '40001'){
Cache::set('ACCESS_TOKEN','');
$this->wxsend($openid,$template_id,$urls,$first,$keyword1,$keyword2,$keyword3,$remark);
}
// dump($output);
}
微信模板消息封装方法
猜你喜欢
转载自blog.csdn.net/weixin_43507521/article/details/85694894
今日推荐
周排行