• 微信利用PHP创建自定义菜单的方法


    在使用通用接口前,你需要做以下两步工作:
    1.拥有一个微信公众账号,并获取到appid和appsecret(在公众平台申请内测资格,审核通过后可获得)
    2.通过获取凭证接口获取到access_token
    注意:
    access_token是第三方访问api资源的票据;
    access_token对应于公众号是全局唯一的票据,重复获取将导致上次获取的access_token失效。

    访问下面这个地址(注意替换你的appid和secret):

    https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

    然后在浏览器能看到一下返回信息:
    {"access_token":"这里就是你的access_token","expires_in":7200}


    创建自定义菜单:

    <?php
    header("Content-type: text/html; charset=utf-8");
    define("ACCESS_TOKEN", "这里填入你上面获取到的access_token");
    
    
    //创建菜单
    function createMenu($data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".ACCESS_TOKEN);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
      return curl_error($ch);
    }
    
    curl_close($ch);
    return $tmpInfo;
    
    }
    
    //获取菜单
    function getMenu(){
    return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN);
    }
    
    //删除菜单
    function deleteMenu(){
    return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".ACCESS_TOKEN);
    }
    
    
    
    
    
    $data = '{
         "button":[
         {
              "type":"click",
              "name":"首页",
              "key":"home"
          },
          {
               "type":"click",
               "name":"简介",
               "key":"introduct"
          },
          {
               "name":"菜单",
               "sub_button":[
                {
                   "type":"click",
                   "name":"hello word",
                   "key":"V1001_HELLO_WORLD"
                },
                {
                   "type":"click",
                   "name":"赞一下我们",
                   "key":"V1001_GOOD"
                }]
           }]
    }';
    
    
    
    
    echo createMenu($data);
    //echo getMenu();
    //echo deleteMenu();



     

     
  • 相关阅读:
    C#多线程编程实战1.5检测线程状态
    C#多线程编程实战1.4终止线程
    C#多线程编程实战1.3等待线程
    C#多线程编程实战1.2暂停线程(休眠)
    C#多线程编程实战1.1创建线程
    C#中base的作用
    C#继承
    C#中return的两个作用
    Windows下完全卸载node.js并安装node.js的多版本管理工具nvm-windows
    执行gulp build报错
  • 原文地址:https://www.cnblogs.com/GmrBrian/p/3612985.html
Copyright © 2020-2023  润新知