代码
// 微信消息推送
public function WxSms (Request $request) {
// 获取token
$appid = "公众号appid";
$secret = "公众号secret ";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
$weixin = file_get_contents($url);//通过code换取网页授权access_token
$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
$json = get_object_vars($jsondecode);//转换成数组
// 拼接短消息模板
$template = array(
'touser' => 'openid',
'template_id' => "短消息模板id",
'url' => '点击要跳转的地址',
'topcolor' => "#FF0000",
"data"=>[
'first'=>[
'value'=>'您好,您有一条打卡提醒',
'color'=>'#000'
],
'keyword1'=>[
'value'=>'河北澄怀',
'color'=>'#000'
],
'keyword2'=>[
'value'=>'2021年1月17日 18:00:00',
'color'=>'#000'
],
'keyword3'=>[
'value'=>'企业疫情防控未打卡',
'color'=>'#000'
],
'remark'=>[
'value'=>'本条为测试数据,发给指定的人',
'color'=>'#000'
]
]
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $json['access_token'];
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode($json_template));
$db = curl_exec($ch); //运行curl
curl_close($ch);
dd($db);
}
结果
