public function template()
{
$userOpenid = 'OPENID';//推动用户的微信openid
$appid = 'APPID';
$secret = 'APPSECRET';
//获得access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$wxResult = file_get_contents($url);//获得网页输出
$obj = json_decode($wxResult,true );//解码
$access_token = $obj['access_token'];//网页授权接口调用凭证
//发送模板消息
$pushUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$data = array(
"touser" => $userOpenid,
"template_id" =>"caDPEyxg54MltVVSOw4svG9ociZbPWilxrP6wKNFeV0",
"data" => array(
'first' => ['value' => '新用户注册信息模板'],
'keyword1' => ['value' => 'New Username'],
'keyword2' => ['value' => date('Y-m-d H:i:s')],
'remark' => ['value' => '感谢您的使用,祝您生活愉快'],
)
);
$params = json_encode($data);
$res=$this->curl_post($pushUrl,$params);
print_r($res);
}
//发送post请求
function curl_post($url , $data=array()){
$ch = curl_init();//创建curl请求
curl_setopt($ch, CURLOPT_URL,$url); //设置发送数据的网址
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值,0,直接显示
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
curl_setopt($ch, CURLOPT_POST, 1);//post方法请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post请求发送的数据包
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data,true); //将json数据转成数组
return $data;
}
感谢博主,喝杯咖啡~
感谢博主,喝杯咖啡~