PHP实现微信推送模板消息

原文:https://blog.csdn.net/haoyunyun888/article/details/85857638 
版权声明:本文为博主原创文章,转载请附上博文链接!

案例效果:

应用场景:

模板通知应用场景:

1.撕名牌通知

2.组织人员通知

3.Wps计划任务通知领取会员

4.手机充值模板通知

5.登陆通知

注意:模板ID   openid 

代码实现:

<?php
//http_request 利用curl请求  两个参数   url连接地址   数据信息
function http_request($url,$data){
    $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);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);//执行 
    curl_close($ch);//关闭
    return $output;//返回结果
}
  //moban 的方法  (参数)
  function moban($plan,$user,$time,$openid)
  {
     //生成acces_token
     $appid="wx830b311b3862cdf1";       //填写微信后台的appid
     $appsecret="bf582a3c6d9cce5002ae408e4388efb1";   //填写微信后台的appsecret
     $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
     $json = file_get_contents($url);
     $result=json_decode($json,true);
     $ACCESS_TOKEN=$result['access_token'];
     //模板消息    
    $template=array(
        'touser'=>$openid,
        'template_id'=>"PaiO8uF0OCC3f2U-Y3U7OIaOb2Wmw1G4Ap-eQEkOrb0",    //模板的id
        'url'=>"http://blog.csdn.net/haoyunyun888",
        'topcolor'=>"#FF0000",
        'data'=>array(
            'plan'=>array('value'=>urlencode($plan),'color'=>"#00008B"),   
            'user'=>array('value'=>urlencode($user),'color'=>'#00008B'),  
            'time'=>array('value'=>urlencode($time),'color'=>'#00008B'),   //时间
        )
    );
    $json_template=json_encode($template);
    $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;
    $res=http_request($url,urldecode($json_template));
    print_r($res);
}
//参数:计划  计划事件  plan
//关联联系人       张三      user
//计划事件     2019年1月7日10:06:06    time   
moban("计划事件"," 张三","2019年1月7日10:06:06","o6JVj50zsXWGWa24aILJTcnotjEQ");
 
?>

猜你喜欢

转载自blog.csdn.net/weixin_42980713/article/details/86003465