公众号 发送模板消息

<?php 
        $toUserOpenid = $this->request->post("to_user_openid");
        $config = get_addon_config('equipment');
        if (!$config || !$config['weappid'] || !$config['weappsecret']) {
            $this->error("请在后台插件管理填写小程序配置项");
        }
        $appid ="wx313f1b89b9a212c2";
        $secret ="ba33d7b034b4fcd0829bdc5f94b1212";
//        $secret = $config['weappsecret'];
        $grant_type = 'client_credential';
// 获取access_token
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=$grant_type&appid=$appid&secret=$secret";
        $access_token_json = file_get_contents($url);
        $access_token_array = json_decode($access_token_json, true);
        $access_token = $access_token_array['access_token'];
        //发送模板  
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token";

        //template_id 模版 在微信公众号,新增模板
        $data = json_encode([
            'touser' => $toUserOpenid,
            'template_id' => 'OGVjIOX1tDh8xX4jYkaDWfzW4oFMEpHr7x37w_WIdm0',
            "url"     => "http://weixin.qq.com/download",
            'topcolor'=>"#FF0000",
            'data' => ['character_string16'=>["value"=>"SB202403122"],
                       "thing17"=>[
                           "value"=>"张怡然",
                       ],
                     "thing15"=> [  "value"=>"维修进行中"
                                   ],
                    "time9"=>[
                        "value"=>date("Y-m-d H:i")
                    ],
                    "Money"=>[
                        "value"=>"人民币260.00元",
                        "color"=>"#173177"
                    ],
                    "thing7"=>[
                        "value"=>"智能家电",
                        "color"=>"#173177"
                    ]
        ]
        ]);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        $result = curl_exec($ch);
        if ($result) {
            var_dump($result);die;
            echo "发送成功";
        } else {
            echo "发送失败。";
        }
        curl_close($ch);

  

posted on 2024-07-31 10:03  kevin_yang123  阅读(14)  评论(0编辑  收藏  举报