微信公众号开发(一) -- 自定义菜单 动态菜单

简单修改一下appid、appsecret 代码可直接复制使用

自定义菜单

//获取token值
$appid = ''; //微信支付申请对应的公众号的APPID
$appsecret = ''; 微信支付申请对应的公众号的APP Key
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res = http_curl($url,'get','json');
$access_token = $res['access_token'];

function http_curl($url,$type='get',$res='json',$arr=''){
    //1.实例化curl
    $ch = curl_init();
    //2.设置curl参数
    curl_setopt($ch,CURLOPT_URL,$url);//要访问的url地址
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//对认证证书的来源检查
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//从证书中检查SSL加密算法是否存在
    if($type=='post'){
        curl_setopt($ch, CURLOPT_POST, 1);//发送一个常规的POST请求
        curl_setopt($ch, CURLOPT_POSTFIELDS,$arr);//post提交的数据包
    }
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//获取的信息以文件流的形式返回

    //3.采集

    $output = curl_exec($ch);//执行操作
    if($res=='json'){
        if(curl_errno($ch)){
            return curl_error($ch);
        }else{
            return json_decode($output,true);
        }
    }
    //4.关闭
    curl_close($ch);
}

$jsonmenu='{
     "button":[
       {
           "name":"注册",
           "sub_button":[
            {
               "type":"view",
               "name":"我是阿姨",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"我是客户",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"我要开店",
               "url":"https://www.baidu.com/"
            }]
       },
      {
           "type":"view",
           "name":"预约服务",
           "url":"https://www.baidu.com/"
       },
       {
           "name":"服务介绍",
           "sub_button":[

            {
               "type":"view",
               "name":"产品介绍",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"企业介绍",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"在线客服",
               "url":"https://www.baidu.com/"
            }]
       }]
 }';

//包含Accesstoken.php主要是获得access_token的功能,具体可以查看我的另一篇博客,微信公众号开发获取accesstoken的文章,很简单的。

$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$result = https_request($url, $jsonmenu);
var_dump($result);//查看返回数据

function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

动态菜单
demo是根据标签不同

$appid = ''; //微信支付申请对应的公众号的APPID
$appsecret = ''; 微信支付申请对应的公众号的APP Key
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res = http_curl($url,'get','json');
$access_token = $res['access_token'];

function http_curl($url,$type='get',$res='json',$arr=''){
    //1.实例化curl
    $ch = curl_init();
    //2.设置curl参数
    curl_setopt($ch,CURLOPT_URL,$url);//要访问的url地址
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//对认证证书的来源检查
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//从证书中检查SSL加密算法是否存在
    if($type=='post'){
        curl_setopt($ch, CURLOPT_POST, 1);//发送一个常规的POST请求
        curl_setopt($ch, CURLOPT_POSTFIELDS,$arr);//post提交的数据包
    }
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//获取的信息以文件流的形式返回

    //3.采集

    $output = curl_exec($ch);//执行操作
    if($res=='json'){
        if(curl_errno($ch)){
            return curl_error($ch);
        }else{
            return json_decode($output,true);
        }
    }
    //4.关闭
    curl_close($ch);
}

$jsonmenu='{
     "button":[
      {
           "name":"订单中心",
           "sub_button":[
            {
               "type":"view",
               "name":"发布订单",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"在线接单",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"我的订单",
               "url":"https://www.baidu.com/"
            }]
       },
      {
           "name":"信息中心",
           "sub_button":[
            {
               "type":"view",
               "name":"背景调查",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"客户预约",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"服务人员",
               "url":"https://www.baidu.com/"
            }]
       },
       {
           "type":"view",
           "name":"商户中心",
           "url":"https://www.baidu.com/"
       }],
       "matchrule":{
        "tag_id":"103"
      }
 }';


$url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=".$access_token;
$result = https_request($url, $jsonmenu);
var_dump($result);
function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

删除菜单

// 删除个性化菜单

//获取token值
$appid = ''; //微信支付申请对应的公众号的APPID
$appsecret = ''; 微信支付申请对应的公众号的APP Key
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res = http_curl($url,'get','json');
$access_token = $res['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
$result = https_request($url);

function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页