tp6微信公众号开发者模式自定义菜单

1,参考上篇博客,获取access_token

https://www.cnblogs.com/xiaoyantongxue/p/15803334.html

2:控制器写以下代码

    /*
     * 获取普通access_token
     * */
    public function getToken()
    {
        $accessToken = WechatService::getToken();
        echo $accessToken;
    }
   /**
     * @return string
     * 菜单
     */
    public function menu()
    {
//        获取accessToken
        $accessToken = WechatService::getToken();
        //组装请求的url地址
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $accessToken;
//        $url=sprintf(config('wechat.menu_url'),$accessToken);
        $data = array(
            // button下的每一个元素
            "button" => array(
                //第一个一级菜单
                array('type' => 'click', "name" => "个人简介", "key" => "info"),
                array(
                    "name" => "语言排行",
                    "sub_button" => array(
                        array("name" => 'HTML', "type" => "view",
                            'url' => "http://119.45.22.188/cms/"),
                        array('name' => 'js', 'type' => 'pic_sysphoto', 'key' => 'sysptoto'),
                        array('name' => 'php', 'type' => 'pic_weixin', 'key' => 'pic_weixin')
                    )
                ),
                array('type' => 'click', 'name' => '日记纪要', 'key' => 'content')
            )
        );
// 将数据转换为json格式
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
//        发送post请求
        $result = curlPost($url, $data);
        dump($result);
    }

3:再tp6 common 公共函数中进行封装一份post请求

 

 

 

function curlPost($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);
    // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output,true);
}

4:网页路由进行访问

 

5:打开微信公众号,进行查看效果

 

 

 

 

模板消息的使用:

模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等。不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息。

官方文档:

https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#3

控制器:

1:测试页面进行模板页面的设置

网址:

https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

 

 

 2:控制器调用

 /**
     *模板消息
     */
    public function sendTemplateMsg(){
//        获取access_token
        $accessToken = WechatService::getToken();
//        请求接口
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$accessToken";
        $data = [
//touser就是测试号的微信号
"touser"=>"oCsrJ6UIuOq8PEd2b7YDgpt2WaX0",
//模板id
"template_id"=>"mhOLKr6gU5-ZZkRdkz4ikj9iockb2H5PfGwya36IXqY", "data"=>[ "result"=>[ "value"=>"某某某", // 颜色 "color"=>"red" ], "withdrawMoney"=>[ "value"=>"998" ], "withdrawTime"=>[ "value"=>date("Y-m-d H:i:s",time()) ], "cardInfo"=>[ "value"=>"工商银行(尾号2345)" ], "arrivedTime"=>[ "value"=>date("Y-m-d H:i:s",time()) ], "remark"=>[ "value"=>"欢迎参与活动", "color"=>"red" ], ] ]; $res = curlPost($url,json_encode($data)); print_r($res);die; }

3:走路由

 

 4:微信看效果

 

posted @ 2022-01-18 21:31  王越666  阅读(330)  评论(0编辑  收藏  举报