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




出处:http://www.phpfensi.com/php/20140108/1168.html


相关教程