一、首先要获得接收模板消息用户的 openid 和 accss_token,可参考微信公众号获取用户基本信息
二、发布推送
public function sendMsg()
{
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";
$params = [
'touser' => $openid,
'template_id' => '',
'url' => '',
'data' =>
[
'first' =>
[
'value' => '注册申请已提交,请等待管理员审核。',
'color' => '#173177'
],
'keyword1' =>
[
'value' => '',
'color' => '#173177'
],
'keyword2' =>
[
'value' => date("Y/m/d"),
'color' => '#173177'
],
'remark' =>
[
'value' => '感谢使用',
'color' => '#173177'
],
]
];
$json = json_encode($params,JSON_UNESCAPED_UNICODE);
return $this->curlPost($url, $json);
}
protected function curlPost($url,$data)
{
$ch = curl_init();
$params[CURLOPT_URL] = $url;
$params[CURLOPT_HEADER] = true;
$params[CURLOPT_SSL_VERIFYPEER] = false;
$params[CURLOPT_SSL_VERIFYHOST] = false;
$params[CURLOPT_RETURNTRANSFER] = true;
$params[CURLOPT_POST] = true;
$params[CURLOPT_POSTFIELDS] = $data;
curl_setopt_array($ch, $params);
$content = curl_exec($ch);
curl_close($ch);
}