设置微信公众号菜单

374次阅读
没有评论

代码为

$menu_data = '{
                "button":[
                    {	
                        "name":"小王简介",
                        "type":"click",
                        "key":"test_key_1"
                    },
                    {
                        "name":"菜单",
                        "sub_button":[
                            {	
                                "type":"view",
                                "name":"李路昌",
                                "url":"http://www.liluchang.com/"
                            },
                            {
                                "type": "scancode_waitmsg", 
                                "name": "扫码带提示", 
                                "key": "rselfmenu_0_0", 
                                "sub_button": [ ]
                            }, 
                            {
                                "type": "scancode_push", 
                                "name": "扫码推事件", 
                                "key": "rselfmenu_0_1", 
                                "sub_button": [ ]
                            },
                            {
                                "name": "发送位置", 
                                "type": "location_select", 
                                "key": "rselfmenu_2_0"
                            }
                        ]
                    },
                    {
                        "name": "发图", 
                        "sub_button": [
                            {
                                "type": "pic_sysphoto", 
                                "name": "系统拍照发图", 
                                "key": "rselfmenu_1_0", 
                               "sub_button": [ ]
                             }, 
                            {
                                "type": "pic_photo_or_album", 
                                "name": "拍照或者相册发图", 
                                "key": "rselfmenu_1_1", 
                                "sub_button": [ ]
                            }, 
                            {
                                "type": "pic_weixin", 
                                "name": "微信相册发图", 
                                "key": "rselfmenu_1_2", 
                                "sub_button": [ ]
                            }
                        ]
                    }
                ]
            }';

/**
 * 如果创建成功,返回值应该是{"errcode":0,"errmsg":"ok"},如果失败,错误时的返回 JSON 数据包如下(示例为无效菜单名长度):是{"errcode":40018,"errmsg":"invalid button name size"}
 * 然后转换为数组
 */
$msg = json_decode(http_post_wechat_menu($menu_data),true);
if($msg['errcode'] == 0)
{
    echo "菜单创建成功";
}else
{
    echo "菜单创建失败!";
}


//post 请求微信公众号菜单接口
function http_post_wechat_menu($menu_data)
{
    //获取access_tokrn
    $access_token = get_access_token();
    //微信公众号设置菜单接口网址
    $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$menu_data);
    $res = curl_exec($ch);
    curl_close($ch);
    return $res;
}

//获取微信公众号access_token
function get_access_token()
{
    $appid = 'xxxxxxxxx';
    $appsecret = 'xxxxxxxxxxx';
    //获取access_token官方网址
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
    $access_token = json_decode(file_get_contents($url),true);
    $wechat_access_token = $access_token['access_token'];
    return $wechat_access_token;
}

 

 

李路昌
版权声明:本站原创文章,由 李路昌 2022-10-10发表,共计1699字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)