• 微信自定义菜单类简单开发样例


      微信一旦对接了消息回复接口等,菜单就只能自定义开发了。不能在网页设置,那么这里是什么原理呢?

      其实,关于原理,微信很简单,只是提供了一个修改菜单的入口,然后你把你的access_token和菜单设置数据请求一下就设置或更改了。话不多说,这里封装了一个菜单设置类,里面写了三个方法,一个access_token方法、一个postcurl方法还有一个主方法createmenu方法(里面设置菜单数据和请求结果回复操作)。一起来看代码吧!

      

     1 <?php
     2 namespace appadmincontroller;
     3 class  Wxmenu
     4 {
     5     private $APPID = "wx18c8353ad80c3511";//配置自己的appid
     6     private $APPSECRET = "699092c6b61cc4204b2365c6dffc0d847";//配置自己的appscreat
     7 
     8     //获取access_token
     9     public function index()
    10     {
    11         $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->APPID . "&secret=" . $this->APPSECRET;
    12         $date = $this->postcurl($url);
    13         $access_token = $date['access_token'];
    14         return $access_token;
    15     }
    16 
    17     //拼接参数,带着access_token请求创建菜单的接口
    18     public function createmenu()
    19     {
    20 
    21         $data = '{
    22         "button":[
    23 
    24       {
    25           "type":"click",
    26                "name":"免费码获取",
    27                "key":"freekey"
    28       },
    29 
    30       {
    31           "name":"休闲时间",
    32            "sub_button":[
    33             {
    34                 "type":"view",
    35                "name":"百度一下",
    36                "url":"http://www.baidu.com/"
    37             },
    38             {
    39                 "type":"view",
    40                "name":"qq音乐",
    41                "url":"https://y.qq.com/n/yqq/toplist/4.html"
    42             },
    43             {
    44                 "type":"view",
    45                "name":"腾讯新闻",
    46                "url":"http://qq.com"
    47             }]
    48        },
    49 
    50         {
    51             "name":"生活助手",
    52            "sub_button":[
    53             {
    54                 "type":"view",
    55                "name":"近日天气",
    56                "url":"http://www.weather.com.cn/"
    57             },
    58             {
    59                 "type":"view",
    60                "name":"百度地图",
    61                "url":"https://map.baidu.com/"
    62             },
    63             {
    64                 "type":"view",
    65                "name":"360安全卫士",
    66                "url":"http://360.cn"
    67             }]
    68         }
    69        ]
    70  }';
    71         $access_token = $this->index();
    72         $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
    73         $result = $this->postcurl($url, $data);
    74         if($result['errcode']==0&&$result['errmsg']=='ok'){
    75             echo "恭喜您,菜单创建成功!!".date('Y-m-d H:m:s',time());
    76         }else{
    77             echo $result;
    78         }
    79     }
    80 
    81     //请求接口方法
    82     function postcurl($url,$data = null)
    83     {
    84         $ch = curl_init();
    85         curl_setopt($ch, CURLOPT_URL, $url);
    86         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    87         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    88         if (!empty($data)) {
    89             curl_setopt($ch, CURLOPT_POST, TRUE);
    90             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    91         }
    92         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    93         $output = curl_exec($ch);
    94         curl_close($ch);
    95         return $output = json_decode($output, true);
    96     }
    97 
    98     }
  • 相关阅读:
    Day5.打包解压,分区,软链接和硬链接
    Day4.权限命令
    Day3.linux基础命令
    macos proxy_bypass_macosx_sysconf exception
    网站收集
    django test
    django test, app aren't loaded yet
    sqlalchemy insert on duplicate update
    linux随机字符串
    Celery Received unregistered task of type
  • 原文地址:https://www.cnblogs.com/phper12580/p/9522277.html
Copyright © 2020-2023  润新知