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

  • 相关阅读:
    重启远程windows计算机
    web.xml文件的作用及基本配置
    DB2编码问题而导致连接不上上数据库
    合理使用表空间
    自动做题
    怎么给二年级小学生讲鸡兔同笼问题
    DB2 性能分析工具介绍:Event Monitor 篇(摘自IBM官方)
    delphi 对应 c# 的一些函数及类型的转换方法(从网络摘录)
    DB2 基础: 使用重定向恢复克隆 DB2 数据库(部分从IBM官方网站引用)
    response.setContentType()的作用及MIME参数详解
  • 原文地址:https://www.cnblogs.com/fps2tao/p/8661322.html
Copyright © 2011-2022 走看看