Skip to content

创建菜单

向军大叔每晚八点在 抖音bilibli 直播

xj-small

以下代码是创建普通菜单,但创建扫码推事件、弹出系统拍照发图等菜单只需要构造相应数据即可。

创建菜单

$button = [
    'button' => [
        0 => [
            'type' => 'view',
            'name' => '开源框架',
            'url'  => 'http://www.hdphp.com/',
        ],
        1 => [
            'name'       => '后盾',
            'sub_button' => [
                [
                    'type' => 'view',
                    'name' => '后盾网',
                    'url'  => 'http://www.houdunwang.com/',
                ],
                [
                    'type' => 'view',
                    'name' => '后盾人',
                    'url'  => 'http://www.houdunren.com/',
                ],
            ],
        ],
    ],
];
WeChat::instance('button')->create($button);

返回值

正确时的返回如下

[
	"errcode"=>0,
	"errmsg"=>"ok"
]

删除菜单

使用接口创建自定义菜单后,开发者还可使用接口删除当前使用的自定义菜单。另请注意,在个性化菜单时,调用此接口会删除默认菜单及全部个性化菜单。

操作实例

WeChat::instance('button')->flush();

返回值

[
	"errcode"=>0,
	"errmsg":"ok"
]

菜单事件

用户点击自定义菜单后,微信会把点击事件推送给开发者,点击一级菜单弹出子菜单,不会产生上报。

点击菜单拉取消息时的事件推送

//消息管理模块
$instance = WeChat::instance('button');
	//关注用户扫描二维码事件
if ($instance->isClickEvent()) {
	//获取消息内容
	$message = $instance->getMessage();
	//向用户回复消息
	return WeChat::instance('message')->text("点击了菜单,EventKey: {$message->EventKey}");
}

点击菜单跳转链接时的事件推送

//消息管理模块
$instance = WeChat::instance('button');
//关注用户扫描二维码事件
if ($instance->isViewEvent()) {
	//获取消息内容
	$message = $instance->getMessage();
	file_put_contents('hd.php',$message->EventKey);
}

扫码推事件的事件推送

//消息管理模块
$instance = WeChat::instance('button');
if ($instance->isScancodePush()){
	//处理的代码
}

扫码推事件且弹出“消息接收中”提示框的事件推送

//消息管理模块
$instance = WeChat::instance('button');
if ($instance->isScancodeWaitmsg()){
	//处理的代码
}

弹出系统拍照发图的事件推送

//消息管理模块
$instance = WeChat::instance('button');
if ($instance->isPicSysphoto()){
	//处理的代码
}

弹出拍照或者相册发图的事件推送

//消息管理模块
$instance = WeChat::instance('button');
if ($instance->isPicPhotoOrAlbum()){
	//处理的代码
}

弹出微信相册发图器的事件推送

//消息管理模块
$instance = WeChat::instance('button');
if ($instance->isPicWeixin()){
	//处理的代码
}

弹出地理位置选择器的事件推送

//消息管理模块
$instance = WeChat::instance('button');
if ($instance->isLocationSelect()){
	//处理的代码
}

查询菜单

使用接口创建自定义菜单后,开发者还可使用接口查询自定义菜单的结构。另外请注意,在设置了个性化菜单后,使用本自定义菜单查询接口可以获取默认菜单和全部个性化菜单信息。

WeChat::instance('button')->query();

返回值 如果有个性菜单,返回数据将包括 conditionalmenu 字段,内容就是个性菜单数据。

Array
(
    [menu] => Array
        (
            [button] => Array
                (
                    [0] => Array
                        (
                            [type] => view
                            [name] => 开源框架
                            [url] => http://www.hdphp.com/
                            [sub_button] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [name] => 后盾
                            [sub_button] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => view
                                            [name] => 后盾网
                                            [url] => http://www.houdunwang.com/
                                            [sub_button] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [type] => view
                                            [name] => 后盾人
                                            [url] => http://www.houdunren.com/
                                            [sub_button] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

个性化菜单

为了帮助公众号实现灵活的业务运营,微信公众平台新增了个性化菜单接口,开发者可以通过该接口,让公众号的不同用户群体看到不一样的自定义菜单。该接口开放给已认证订阅号和已认证服务号。

创建菜单

$data = [
            'button'    => [
                [
                    'type' => 'click',
                    'name' => '今日歌曲',
                    'key'  => 'V1001_TODAY_MUSIC',
                ],
                [
                    'name'       => '菜单',
                    'sub_button' =>
                        [
                            [
                                'type' => 'view',
                                'name' => '搜索',
                                'url'  => 'http://www.soso.com/',
                            ]
                        ],
                ],
            ],
            'matchrule' => [
                'tag_id'               => '2',
                'sex'                  => '1',
                'country'              => '中国',
                'province'             => '广东',
                'city'                 => '广州',
                'client_platform_type' => '2',
                'language'             => 'zh_CN',
            ]
];
WeChat::instance('button')->createSpecialButton($data);

返回结果

[
	"menuid"=>"208379533"
]

删除菜单

删除个性菜单需要提供菜单编号(menuid),可以通过自定义菜单查询接口获取。

$res = WeChat::instance('button')->delSpecialButton($res['menuid']);

返回结果

[
	"errcode"=>0,
	"errmsg":"ok"
]

测试菜单

该接口将返回菜单配置。参数为 user_id 可以是粉丝的 OpenID,也可以是粉丝的微信号。

WeChat::instance('button')->trySpecialButton('houdunwangxj')

查询菜单

使用普通自定义菜单查询接口可以获取默认菜单和全部个性化菜单信息,请见自定义菜单查询接口的说明。

删除菜单

使用普通自定义菜单删除接口可以删除所有自定义菜单(包括默认菜单和全部个性化菜单),请见自定义菜单删除接口的说明。

获取菜单配置

本接口将会提供公众号当前使用的自定义菜单的配置,如果公众号是通过 API 调用设置的菜单,则返回菜单的开发配置,而如果公众号是在公众平台官网通过网站功能发布菜单,则本接口返回运营者设置的菜单配置。

WeChat::instance('button')->getCurrentSelfMenuInfo();

具体返回值可查看微信官方文档。