PHP微信公众号发送模板消息
微信发送模板消息
代码:
public function sendMessage($wx_openid, $coupon_code)
{
$data=[
'touser'=>$wx_openid,//用户openid
'template_id'=>'ksi6ipQPcsBMmSBQmsXZdYf0wlZseG4OBnvfrVLZglM',//模板id
'topcolor'=>"#FF0000",
'data'=>array(
'name'=>array('value'=>$coupon_code,'color'=>"#008000"),//模板对应参数
'remark'=>array('value'=>'请尽快兑换,否则视为放弃','color'=>'#173177'),//模板对应参数
)];
Loader::import('dodgepudding.wechat-php-sdk.wechat', VENDOR_PATH, '.class.php');
$config = Db::name('wxpay')->find();
$options['appid'] = $config['wxappid'];
$options['appsecret'] = $config['wxappsecret'];
// $options['appid'] = '';//微信appid
// $options['appsecret'] = '';//微信appsecret
$wx=new \wechat($options);
$re = $wx->sendTemplateMessage($data);
if($re){
return true;
}else{
write_log('sendMessage',"发送失败".json_encode($re));
return true;
}
}
要在白名单里添加ip地址,要不会发送失败