• 微信公众号开发之创建菜单栏代码示例(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

  • 相关阅读:
    linux全方位掌握一个命令--思路比方法更重要
    grep命令详解
    linux中的通配符与正则表达式
    sed命令使用详解归纳
    linux下命令行操作快捷键及技巧
    (原创)发布一个C++版本的ORM库SmartDB(一)
    (原创)C++11改进我们的程序之简化我们的程序(八)
    (原创)C++11改进我们的程序之简化我们的程序(七)
    (原创)C++11改进我们的程序之简化我们的程序(六)
    (原创)C++11改进我们的程序之简化我们的程序(五)
  • 原文地址:https://www.cnblogs.com/fps2tao/p/8661322.html
Copyright © 2020-2023  润新知