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

原文地址:https://www.cnblogs.com/fps2tao/p/8661322.html