当前位置:首页 > PHP教程 > php类库 > 列表

php实现微信公众平台账号自定义菜单类

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-02 20:13:33 浏览: 评论:0 

这篇文章主要介绍了php实现微信公众平台账号自定义菜单类的方法,涉及针对微信接口API的二次开发,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了php实现微信公众平台账号自定义菜单类的方法。分享给大家供大家参考。具体分析如下:

微信公众平台服务号可申请自定义菜单了,其它的号暂时不支持自定义菜单了,这个不但可以使用api来操作,还可以直接在后台定义菜单与参数。

申请自定义菜单

服务号可以申请自定义菜单,使用QQ登录的公众号,可以升级为邮箱登录,使用邮箱登录的公众号,可以修改登录邮箱,群发消息可以同步到腾讯微博.

微信公众平台账号api程序,代码如下:

  1. <?php 
  2. //define your token 
  3. define("TOKEN""chenxiang");//改成自己的TOKEN 
  4. define('APP_ID''');//改成自己的APPID 
  5. define('APP_SECRET''');//改成自己的APPSECRET 
  6.  
  7. $wechatObj = new wechatCallbackapiTest(APP_ID,APP_SECRET); 
  8. $wechatObj->Run(); 
  9.  
  10. class wechatCallbackapiTest 
  11.     private $fromUsername
  12.     private $toUsername
  13.     private $times
  14.     private $keyword
  15.     private $app_id
  16.     private $app_secret
  17.      
  18.     public function __construct($appid,$appsecret
  19.     { 
  20.         # code... 
  21.         $this->app_id = $appid
  22.         $this->app_secret = $appsecret
  23.     } 
  24.     public function valid() 
  25.     { 
  26.         $echoStr = $_GET["echostr"]; 
  27.         if($this->checkSignature()){ 
  28.             echo $echoStr
  29.             exit
  30.         } 
  31.     } 
  32.     /** 
  33.      * 运行程序 
  34.      * @param string $value [description] 
  35.      */ 
  36.     public function Run() 
  37.     { 
  38.         $this->responseMsg(); 
  39.         $arr[]= "您好,这是自动回复,我现在不在,有事请留言,我会尽快回复你的^_^"
  40.         echo $this->make_xml("text",$arr); 
  41.     } 
  42.     public function responseMsg() 
  43.     {    
  44.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//返回回复数据 
  45.         if (!emptyempty($postStr)){ 
  46.                 $access_token = $this->get_access_token();//获取access_token 
  47.                 $this->createmenu($access_token);//创建菜单 
  48.                 //$this->delmenu($access_token);//删除菜单 
  49.                 $postObj = simplexml_load_string($postStr'SimpleXMLElement', LIBXML_NOCDATA); 
  50.                 $this->fromUsername = $postObj->FromUserName;//发送消息方ID 
  51.                 $this->toUsername = $postObj->ToUserName;//接收消息方ID 
  52.                 $this->keyword = trim($postObj->Content);//用户发送的消息 
  53.                 $this->times = time();//发送时间 
  54.                 $MsgType = $postObj->MsgType;//消息类型 
  55.                 if($MsgType=='event'){ 
  56.                     $MsgEvent = $postObj->Event;//获取事件类型 
  57.                     if ($MsgEvent=='subscribe') {//订阅事件 
  58.                         $arr[] = "你好,我是xxx,现在我们是好友咯![愉快][玫瑰]"
  59.                         echo $this->make_xml("text",$arr); 
  60.                         exit
  61.                     }elseif ($MsgEvent=='CLICK') {//点击事件 
  62.                         $EventKey = $postObj->EventKey;//菜单的自定义的key值,可以根据此值判断用户点击了什么内容,从而推送不同信息 
  63.                         $arr[] = $EventKey
  64.                         echo $this->make_xml("text",$arr); 
  65.                         exit
  66.                     } 
  67.                 } 
  68.         }else { 
  69.             echo "this a file for weixin API!"
  70.             exit
  71.         } 
  72.     } 
  73.     /** 
  74.      * 获取access_token 
  75.      */ 
  76.     private function get_access_token() 
  77.     { 
  78.         $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->app_id."&secret=".$this->app_secret; 
  79.         $data = json_decode(file_get_contents($url),true); 
  80.         if($data['access_token']){ 
  81.             return $data['access_token']; 
  82.         }else
  83.             return "获取access_token错误"
  84.         } 
  85.     } 
  86.     /** 
  87.      * 创建菜单 
  88.      * @param $access_token 已获取的ACCESS_TOKEN 
  89.      */ 
  90.     public function createmenu($access_token
  91.     { 
  92.         $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token
  93.         $arr = array(  
  94.             'button' =>array
  95.                 array
  96.                     'name'=>urlencode("生活查询"), 
  97.                     'sub_button'=>array
  98.                         array
  99.                             'name'=>urlencode("天气查询"), 
  100.                             'type'=>'click'
  101.                             'key'=>'VCX_WEATHER' 
  102.                         ), 
  103.                         array
  104.                             'name'=>urlencode("身份证查询"), 
  105.                             'type'=>'click'
  106.                             'key'=>'VCX_IDENT' 
  107.                         ) 
  108.                     ) 
  109.                 ), 
  110.                 array
  111.                     'name'=>urlencode("轻松娱乐"), 
  112.                     'sub_button'=>array
  113.                         array
  114.                             'name'=>urlencode("刮刮乐"), 
  115.                             'type'=>'click'
  116.                             'key'=>'VCX_GUAHAPPY' 
  117.                         ), 
  118.                         array
  119.                             'name'=>urlencode("幸运大转盘"), 
  120.                             'type'=>'click'
  121.                             'key'=>'VCX_LUCKPAN' 
  122.                         ) 
  123.                     ) 
  124.                 ), 
  125.                 array
  126.                     'name'=>urlencode("我的信息"), 
  127.                     'sub_button'=>array
  128.                         array
  129.                             'name'=>urlencode("关于我"), 
  130.                             'type'=>'click'
  131.                             'key'=>'VCX_ABOUTME' 
  132.                         ), 
  133.                         array
  134.                             'name'=>urlencode("工作信息"), 
  135.                             'type'=>'click'
  136.                             'key'=>'VCX_JOBINFORMATION' 
  137.                         ) 
  138.                     ) 
  139.                 ) 
  140.             ) 
  141.         ); 
  142.         $jsondata = urldecode(json_encode($arr)); 
  143.         $ch = curl_init(); 
  144.         curl_setopt($ch,CURLOPT_URL,$url); 
  145.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  146.         curl_setopt($ch,CURLOPT_POST,1); 
  147.         curl_setopt($ch,CURLOPT_POSTFIELDS,$jsondata); 
  148.         curl_exec($ch); 
  149.         curl_close($ch); 
  150.     } 
  151.     /** 
  152.      * 查询菜单 
  153.      * @param $access_token 已获取的ACCESS_TOKEN 
  154.      */ 
  155.      
  156.     private function getmenu($access_token
  157.     { 
  158.         # code... 
  159.         $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token
  160.         $data = file_get_contents($url); 
  161.         return $data
  162.     } 
  163.     /** 
  164.      * 删除菜单 
  165.      * @param $access_token 已获取的ACCESS_TOKEN 
  166.      */ 
  167.      
  168.     private function delmenu($access_token
  169.     { 
  170.         # code... 
  171.         $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token
  172.         $data = json_decode(file_get_contents($url),true); 
  173.         if ($data['errcode']==0) { 
  174.             # code... 
  175.             return true; 
  176.         }else
  177.             return false; 
  178.         } 
  179.     } 
  180.          
  181.     /** 
  182.      *@param type: text 文本类型, news 图文类型 
  183.      *@param value_arr array(内容),array(ID) 
  184.      *@param o_arr array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID) 
  185.      */ 
  186.      
  187.     private function make_xml($type,$value_arr,$o_arr=array(0)){ 
  188.         //=================xml header============ 
  189.         $con="<xml> 
  190.                     <ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName> 
  191.                     <FromUserName><![CDATA[{$this->toUsername}]]></FromUserName> 
  192.                     <CreateTime>{$this->times}</CreateTime> 
  193.                     <MsgType><![CDATA[{$type}]]></MsgType>"; 
  194.                      
  195.           //=================type content============ 
  196.         switch($type){ 
  197.            
  198.             case "text" :  
  199.                 $con.="<Content><![CDATA[{$value_arr[0]}]]></Content> 
  200.                     <FuncFlag>{$o_arr}</FuncFlag>";   
  201.             break
  202.              
  203.             case "news" :  
  204.                 $con.="<ArticleCount>{$o_arr[0]}</ArticleCount> 
  205.                      <Articles>"; 
  206.                 foreach($value_arr as $id=>$v){ 
  207.                     if($id>=$o_arr[0]) breakelse null; //判断数组数不超过设置数 
  208.                     $con.="<item> 
  209.                          <Title><![CDATA[{$v[0]}]]></Title>  
  210.                          <Description><![CDATA[{$v[1]}]]></Description> 
  211.                          <PicUrl><![CDATA[{$v[2]}]]></PicUrl> 
  212.                          <Url><![CDATA[{$v[3]}]]></Url> 
  213.                          </item>"; 
  214.                 } 
  215.                 $con.="</Articles> 
  216.                      <FuncFlag>{$o_arr[1]}</FuncFlag>";   
  217.             break
  218.         } //end switch 
  219.          //=================end return============ 
  220.         $con.="</xml>"
  221.         return $con
  222.     } 
  223.  
  224.     private function checkSignature() 
  225.     { 
  226.         $signature = $_GET["signature"]; 
  227.         $timestamp = $_GET["timestamp"]; 
  228.         $nonce = $_GET["nonce"];     
  229.                  
  230.         $token = TOKEN; 
  231.         $tmpArr = array($token$timestamp$nonce); 
  232.         sort($tmpArr); 
  233.         $tmpStr = implode( $tmpArr ); 
  234.         $tmpStr = sha1( $tmpStr ); 
  235.          
  236.         if$tmpStr == $signature ){ 
  237.             return true; 
  238.         }else
  239.             return false; 
  240.         } 
  241.     } 
  242. ?> 

希望本文所述对大家基于PHP的微信开发有所帮助。

Tags: php微信公众平台菜单类

分享到: