微信公众号模板消息推送

 微信公众号模板消息推送

 

下面介绍一下流程

1.首先先获取access_token

2.获取到了access_token,那么就去微信的模板库里挑选模板,并且把它添加到我的模板里面

3.接下来就是编辑数据,传递过去即可

4.当你发送了数据之后,微信会把推送成功的信息发送到你公众号基本配置里的服务器配置里配置的地址上面去

具体代码如下:

 

 1     //推送消息
 2     public function push(){
 3         $access_token = $this->access_token();
 4         if ($access_token == 400){
 5             return json(['code'=>0,'message'=>'获取access_token失败']);
 6         }$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
 7    $data = array(
 8             "touser"        =>  $openid,        //openid
 9             "template_id"   =>  '模板id',             //模板id
10             "url"           =>  '用户点击信息跳转地址',
11             "data"          =>  array(
12                                     'first' => array(
13                                         'value'=>'尊敬的客户,您的订单有了新的进展',
14                                         'color'=>''
15                                     ),
16                                     'keyword1'=>array(
17                                         'value'=>123465,
18                                         'color'=>''
19                                     ),
20                                     'keyword2'=>array(
21                                         'value'=>'订单进度提醒',
22                                         'color'=>''
23                                     ),
24                                     'keyword3'=>array(
25                                         'value'=>66666,
26                                         'color'=>''
27                                     ),
28                                     'keyword4'=>array(
29                                         'value'=>date('Y-m-d H:i:s', time()),
30                                         'color'=>''
31                                     ),
32                                     'remark'    => array(
33                                         'value'=>'最新订单状态',
34                                         'color'=>''
35                                     ),
36                                 ),        //模板数据
37         );
38         return $this->http_post_json($url,json_encode($data));//发送请求
39     }
40 
41     
42     public function http_post_json($url, $jsonStr)
43     {
44         $ch = curl_init();
45         curl_setopt($ch, CURLOPT_POST, 1);
46         curl_setopt($ch, CURLOPT_URL, $url);
47         curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
48         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
49         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
50                 'Content-Type: application/json; charset=utf-8',
51                 'Content-Length: ' . strlen($jsonStr)
52             )
53         );
54         $response = curl_exec($ch);
55         $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
56         curl_close($ch);
57 
58         return json_encode(array($httpCode, $response),true);
59     }
60 
61     //获取access_token
62     public function access_token(){
63         $access_token = Cache::get('access_token');
64         if (!empty($access_token)){
65             return $access_token;
66         }
67         $appId = APPID;
68         $appSecret = appsecret;
69         $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
70         $ch = curl_init();//初始化curl
71         curl_setopt($ch, CURLOPT_URL,$url); //要访问的地址
72         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
73         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证
74         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
75         $data = json_decode(curl_exec($ch),true);
76         if(curl_errno($ch)){
77             var_dump(curl_error($ch)); //若错误打印错误信息
78         }
79 
80         curl_close($ch);//关闭curl
81         if(Cache::set('access_token',$data['access_token'],172800)){
82             return $data['access_token'];
83         }else{
84             return 400;
85         }
86     }

 

原文地址:https://www.cnblogs.com/junyi-bk/p/12427026.html#4559620

posted @ 2020-07-09 21:20  养猪至富  阅读(343)  评论(0编辑  收藏  举报