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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41654694/article/details/83619651

简单修改一下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;
}
展开阅读全文

微信公众号开发自定义菜单

09-09

用的是github上开源的wechat.class.php sdk。index.php文件,也是我在公众号服务器配置的URL地址,index.php代码如下:rnrn[code=php]rn'123456', //填写你设定的keyrn 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey,如接口为明文模式可忽略rn 'appid'=>'xxxxxxx', //appidrn 'appsecret'=>'xxxxxxx',rn 'debug'=>true,rn 'logcallback'=>'user_dump' //日志文件rn);rnrnrn$weObj = new Wechat($options);rn$ret = $weObj->valid();//明文或兼容模式可以在接口验证通过后注释此句,但加密模式一定不能注释,否则会验证失败rnif (!$ret) rn user_dump("验证失败");rn //exit;rnrnrn //设置菜单rn $newmenu = array (rn 'button' => array (rn 0 => array (rn 'name' => '我是商户',rn 'sub_button' => array (rn 0 => array (rn 'type' => 'view',rn 'name' => '绑定',rn 'url' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx219d0736743e4699&redirect_uri=http://g1t5549946.imwork.net/inn_wechat_nofity/bind.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect',rn ),rn 1 => array (rn 'type' => 'view',rn 'name' => '解绑',rn 'url' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx219d0736743e4699&redirect_uri=http://g1t5549946.imwork.net/inn_wechat_nofity/unbind.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect',rn ),rn 2 => array (rn 'type' => 'view',rn 'name' => '订单查询',rn 'url' => 'http://g1t5549946.imwork.net/inn_wechat_nofity/order_query.php',rn ),rn ),rn ),rn )rn );rnrn $result = $weObj->createMenu($newmenu);rnrnrn$type = $weObj->getRev()->getRevType();rn$events = $weObj->getRevEvent();rnswitch($type) rn case Wechat::MSGTYPE_TEXT:rn $weObj->text("hello, I'm wechat")->reply();rn exit;rn break;rn case Wechat::MSGTYPE_IMAGE:rn break;rn case Wechat::MSGTYPE_EVENT:rn if($events['event'] == Wechat::EVENT_SUBSCRIBE)//首次关注rn rn rn rn elseif ($events['event'] == Wechat::EVENT_UNSUBSCRIBE)rn rn //取消关注rn rn break;rn default:rn $weObj->text("help info")->reply();rn break;rnrnrn[/code]rnrn我的问题是,这个createmenu方法可以创建一个自定义的菜单,但是index.php这个页面会被经常调用(比如关注着给公众号发消息),总不能每次都去调用createmenu吧? 菜单只要一次创建成功了,后面就不用不断的调用了吧?如何在代码中控制呢?rnrn希望我说明白了。 论坛

没有更多推荐了,返回首页