失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > php微信 消息推送 配置 PHP微信公众号模板消息推送

php微信 消息推送 配置 PHP微信公众号模板消息推送

时间:2020-10-21 22:50:13

相关推荐

php微信 消息推送  配置 PHP微信公众号模板消息推送

1,进入微信公众平台查看模板,如果在功能下面看不到模板消息的话,点击添加功能插件,开通消息模板;

2,添加号模板之后记录好模板消息id;

3,发送模板消息的方法

/**

* 发送模板消息

*/

protected function send_notice($json_template){

//获取access_token

if (Session::get('access_token')){

$access_token2=Session::get('access_token');

}else{

$appid = 'xxxxxxx';//微信公众号的appid

$appsecret = 'xxxxxxxxxxxxxxxxxxxxxx';//微信公众号的appsecret

$jsontoken=$this->curl_post("https://api./cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appsecret}");

$access_token1=json_decode($jsontoken,true);

$access_token2=$access_token1['access_token'];

Session::set('access_token', $access_token2);

//setcookie('access_token',$access_token2,7200);

}

//模板消息

//$url = "https://api./cgi- bin/message/template/send?access_token=".$access_token2;

$url = "https://api./cgi-bin/message/template/send?access_token=${access_token2}";

$res = $this->curl_post($url,urldecode($json_template));

return $res;

/*if ($res['errcode']==0){

return '发送成功';

}else{

return '发送失败';

}*/

}

复制代码

4,将模板消息json格式化

/**

* 将模板消息json格式化

*/

protected function json_tempalte($first,$keyword1,$keyword2,$openid,$tempalate_id){

//模板消息

$template=array(

'touser'=>$openid, //用户openid

'template_id'=>$tempalate_id, //在公众号下配置的模板id

'url'=>"", //点击模板消息会跳转的链接

'topcolor'=>"#7B68EE",

'data'=>array(

'first'=>array('value'=>urlencode($first),'color'=>"#173177"),

'keyword1'=>array('value'=>urlencode($keyword1),'color'=>'#173177'), //keyword需要与配置的模板消息对应

'keyword2'=>array('value'=>urlencode($keyword2),'color'=>'#173177'),

//'keyword3'=>array('value'=>urlencode('测试发布人'),'color'=>'#173177'), //'keyword4'=>array('value'=>urlencode('测试状态'),'color'=>'#173177'), 'remark' =>array('value'=>urlencode('请及时联系客户进行续约或退房'),'color'=>'#173177'), )

);

$json_template=json_encode($template);

return $json_template;

}

复制代码

5,请求的方法

/**

* @param $url

* @param array $data

* @return mixed

* curl请求

*/

protected function curl_post($url , $data=array()){

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

// POST数据

curl_setopt($ch, CURLOPT_POST, 1);

// 把post的变量加上

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$output = curl_exec($ch);

curl_close($ch);

return $output;

}

复制代码

6,查询要推送的数据调用推送的方法(根据自己的实际情况查询数据),也可放进脚本满足条件的每天进行推送,就完成了。

protected function warn()

{

$err = [];//记录推送失败的记录

$tempalate_id = 'xxxxxxxxxxx';//公众号消息模板

//收房合同

$o_fifteen = ['TIMESTAMPDIFF(DAY,DATE_FORMAT(NOW(), "%Y-%m-%d"),time)' => 15];

$o_thirty = ['TIMESTAMPDIFF(DAY,DATE_FORMAT(NOW(), "%Y-%m-%d"),time)' => 30];

$fif_ocontract = Db::name('table')->where('status',5)->where('salement',257)->where($o_fifteen)->select(); if (!empty($fif_ocontract)) {

foreach ($fif_ocontract as $k => $v) {

$manager = Db::name('table')->where('hid',$v['house_id'])->value('manager');

$staff = Db::name('table')->field('field')->where('id',$manager)->find(); $first = "亲爱的管房人,您有客户的合同即将到期";

$keyword1 = "合同编号";

$keyword2 = "到期时间";

$json_template = $this->json_tempalte($first,$keyword1,$keyword2,$staff['openid'],$tempalate_id);

$res = $this->send_notice( $json_template);

printLn("{$res}");

if ($res['errcode']!=0){

$err[] = '【'.$v['id'].'】合同到期推送失败';

}

}

}

if (!empty($err)) $this->_error('合同到期提醒推送失败,'.var_export($err,true));

$this->_succ('合同到期提醒推送成功');

}

复制代码

注意:在获取access_token有可能提示:

{"errcode":40013,"errmsg":"invalid appid rid: 5f87c480-202828a3-273742df"}

复制代码

要注意请求的url对appid和appsecret的拼接,不是直接appid而是appid 而是appid而是{appid}

在请求发送模板消息时可能提示:

bin/message/template/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

HTTP/1.1 404 Not Found

Connection: close

Date: Thu, 15-Oct- 03:50:52 GMT Content-Length: 0

复制代码

也是要注意参数拼接问题,如:不能像url1一样拼接,正确的写法是像url2。

//$url = "https://api./cgi- bin/message/template/send?access_token=".$access_token2;

$ur2 = "https://api./cgi-bin/message/template/send?access_token=${access_token2}";

复制代码

如果觉得《php微信 消息推送 配置 PHP微信公众号模板消息推送》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。