zoukankan      html  css  js  c++  java
  • 微信公众号开发之创建菜单栏代码示例(php)

    思路很简单:就是先获取access_token,然后带着一定规则的json数据参数请求创建菜单的接口。废话不多讲,直接上代码。

    class  Wechat   
    {      
        public $APPID="wx******596";      
        public $APPSECRET="ad******0";  
        //获取access_token  
        public function index()  
        {         
            $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET;        
            $date=postcurl($url);  
            $access_token=$date['access_token'];  
            return $access_token;         
        }  
        //拼接参数,带着access_token请求创建菜单的接口  
        public function createmenu(){  
              
          $data='{  
         "button":[  
           
          {      
                   "type":"view",  
                   "name":"精选课程",  
                   "url":"https://w.url.cn/s/ASOsHnk"  
          },       
           
          {  
               "name":"优研优选",  
               "sub_button":[  
                {      
                   "type":"click",  
                   "name":"院校&导师",  
                   "key":"SCHOOCL_TEACHER"  
                },  
                {  
                   "type":"view",  
                   "name":"快速登录",  
                   "url":"http://www.uyanuxuan.com/index.php"  
                },  
                {  
                   "type":"view",  
                   "name":"导师计划",  
                   "url":"http://www.uyanuxuan.com/index.php/Home/About/xsjh.html"  
                }]  
           },  
             
            {  
               "name":"我的",  
               "sub_button":[  
                {      
                   "type":"click",  
                   "name":"联系我们",  
                   "key":"CONTACTUS"  
                },  
                {  
                   "type":"view",  
                   "name":"正版软件",  
                   "url":"http://www.xmypage.com/model2_37685.html"  
                },  
                {  
                   "type":"view",  
                   "name":"四六级冲刺",  
                   "url":"https://h5.youyinian.cn/"  
                }]  
            }        
           ]  
     }';      
         $access_token=$this->index();  
         $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;    
         $result=postcurl($url,$data);  
         var_dump($result);             
        }     

    备注:postcurl方法是提前写好的php请求接口的方法。代码如下:

    //请求接口方法  
    function postcurl($url,$data = null){         
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);   
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
       if (!empty($data)){  
           curl_setopt($ch, CURLOPT_POST, TRUE);  
           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
       }  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    $output = curl_exec($ch);  
    curl_close($ch);  
    return  $output=json_decode($output,true);            
    } 
        public function getCurl($url)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            return $output;
    
    
        }

    转:https://blog.csdn.net/u013077250/article/details/79041303

    自己也有写:https://gitee.com/fps2tao/openweixin

  • 相关阅读:
    Ubuntu 18.04.3 更改系统语言为简体中文
    Centos7.3、nginx环境下部署hugo博客
    Centos7.3 卸载 Nginx(彻底卸载) 并重新安装 Nginx(RPM源yum安装)
    Centos7.3、nginx环境下部署hexo博客(非git推送方式)
    使用阿里云对象存储OSS+PicGo搭建图床
    Hexo博客添加LiveRe评论系统
    使用 jsDelivr CDN加速Github 仓库的图片
    解决win10一开机占用内存就飙到70%的问题
    [Andriod官方训练教程]管理Activity的生命活动之停止和重启一个Activity
    [Andriod官方训练教程]支持不同的设备之支持不同的语言
  • 原文地址:https://www.cnblogs.com/fps2tao/p/8661322.html
Copyright © 2011-2022 走看看