当前位置:首页 > PHP教程 > php高级应用 > 列表

微信公众平台开发通过php创建自定义菜单

发布:smiling 来源: PHP粉丝网  添加日期:2016-08-25 14:46:14 浏览: 评论:0 

自定义菜单可以在后台设置但如果你使用了开发者之后那么后台的自定义菜单功能就失效了,必须通过程序来创建了,下面一起来看一个简单的通过php创建自定义菜单的例子.

验证配置,代码如下:

  1. define("TOKEN""xxx");//改成自己的TOKEN 
  2. define('APP_ID''xxxx');//改成自己的APPID  
  3. define('APP_SECRET''xxx');//改成自己的APPSECRET 
  4. $wechatObj = new wechatCallbackapiTest(); 
  5. if (isset($_GET['echostr'])) { 
  6.     $wechatObj->valid(); 
  7. }else//phpfensi.com 
  8.     $wechatObj->responseMsg(); 
  9. class wechatCallbackapiTest{} 

这个类就介绍了网上多了去了,大家可以自行搜索了下,我只介绍两个创建自定义菜单的函数,代码如下:

  1.   /**  
  2.      * 获取access_token  
  3.      */  
  4.     public function get_access_token()  
  5.     {  
  6.         $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APP_ID."&secret=".APP_SECRET;  
  7.         $data = json_decode(file_get_contents($url),true);  
  8.         if($data['access_token']){  
  9.             return $data['access_token'];  
  10.         }else{  
  11.             return "获取access_token错误";  
  12.         }  
  13.     }  
  14.  
  15.   /**  
  16.      * 创建菜单  
  17.      * @param $access_token 已获取的ACCESS_TOKEN  
  18.      */  
  19.     public function createmenu($access_token)  
  20.     {  
  21.         $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;  
  22.         $arr = array(   
  23.             'button' =>array(  
  24.                 array(  
  25.                     'name'=>urlencode("aaaa"),  
  26.                     'sub_button'=>array(  
  27.                         array(  
  28.                             'name'=>urlencode("bbbb"),  
  29.                             'type'=>'click',  
  30.                             'key'=>'VCX_WEATHER'  
  31.                         ),  
  32.                         array(  
  33.                             'name'=>urlencode("cccc"),  
  34.                             'type'=>'click',  
  35.                             'key'=>'VCX_IDENT'  
  36.                         )  
  37.                     )  
  38.                 ),  
  39.                 array(  
  40.                     'name'=>urlencode("dddd"),  
  41.                     'sub_button'=>array(  
  42.                         array(  
  43.                             'name'=>urlencode("xxxx"),  
  44.                             'type'=>'click',  
  45.                             'key'=>'VCX_GUAHAPPY'  
  46.                         ),  
  47.                         array(  
  48.                             'name'=>urlencode("xxxx"),  
  49.                             'type'=>'click',  
  50.                             'key'=>'VCX_LUCKPAN'  
  51.                         )  
  52.                     )  
  53.                 ),  
  54.                 array(  
  55.                     'name'=>urlencode("xxxx"),  
  56.                     'sub_button'=>array(  
  57.                         array(  
  58.                             'name'=>urlencode("aaaa"),  
  59.                             'type'=>'click',  
  60.                             'key'=>'VCX_ABOUTME'  
  61.                         ),  
  62.                         array(  
  63.                             'name'=>urlencode("aaaaa"),  
  64.                             'type'=>'click',  
  65.                             'key'=>'VCX_JOBINFORMATION'  
  66.                         )  
  67.                     )  
  68.                 )  
  69.             )  
  70.         );  
  71.         $jsondata = urldecode(json_encode($arr));  
  72.         $ch = curl_init();  //phpfensi.com 
  73.         curl_setopt($ch,CURLOPT_URL,$url);  
  74.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
  75.         curl_setopt($ch,CURLOPT_POST,1);  
  76.         curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata);  
  77.         curl_exec($ch);  
  78.         curl_close($ch);  
  79.     }  
  80.  

使用方法是如下代码:

  1. $access_token = $wechatObj->get_access_token();//获取access_token  
  2. $wechatObj->createmenu($access_token);//创建菜单到微信公共平台 

Tags: 公众 菜单 平台

分享到: