zoukankan      html  css  js  c++  java
  • 微信公众号开发笔记2-自定义菜单

    创建菜单的方法

    首先确定你的菜单的JSON结构,比如

    {
         "button":[
         {    
              "type":"click",  "name":"今日歌曲","key":"V1001_TODAY_MUSIC"
          },
          {
               "name":"菜单", "sub_button":[
               {    
                   "type":"view", "name":"搜索", "url":"http://www.soso.com/"
                },
                {
                   "type":"view", "name":"视频", "url":"http://v.qq.com/"
                },
                {
                   "type":"click", "name":"赞一下我们",  "key":"V1001_GOOD"
                }]
           }]
     }

      a)将以下代码保存为menu.php,并且在浏览器中运行该文件(比如 http://1.applinzi.com/menu.php),将直接向微信服务器提交菜单,

    <?php
    
    $appid = "aaaaaa";
    $appsecret = "bbbbbbbbbbbbbbbb";
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
    
    $output = https_request($url);
    $jsoninfo = json_decode($output, true);
    
    $access_token = $jsoninfo["access_token"];
    
    
    $jsonmenu = '{
         "button":[
         {    
              "type":"click",
              "name":"今日歌曲",
              "key":"V1001_TODAY_MUSIC"
          },
          {
               "name":"菜单",
               "sub_button":[
               {    
                   "type":"view",
                   "name":"搜索",
                   "url":"http://www.soso.com/"
                },
                {
                   "type":"view",
                   "name":"视频",
                   "url":"http://v.qq.com/"
                },
                {
                   "type":"click",
                   "name":"赞一下我们",
                   "key":"V1001_GOOD"
                }]
           }]
     }';
    
    
    $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
    $result = https_request($url, $jsonmenu);
    var_dump($result);
    
    function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    
    ?>
    View Code

      b)或者使用官方的调试接口 使用网页调试工具调试该接口

      提交成功后,重新关注后即可看到菜单。

    响应菜单点击事件 

      在消息接口中处理event事件,其中的click代表菜单点击,通过响应菜单结构中的key值回应消息,view事件无须响应,将直接跳转过去

      代码摘自方倍工作室:http://www.cnblogs.com/txw1958/p/weixin-58-custom-menu.html

    <?php
    
    /* 
    【版权声明】 
        本软件产品的版权归方倍工作室所有,受《中华人民共和国计算机软件保护条例》等知识产权法律及国际条约与惯例的保护。您获得的只是本软件的使用权。 
     
        您不得: 
        * 在未得到授权的情况下删除、修改本软件及其他副本上一切关于版权的信息; 
        * 销售、出租此软件产品的任何部分; 
        * 从事其他侵害本软件版权的行为。 
     
        如果您未遵守本条款的任一约定,方倍工作室有权立即终止本条款的执行,且您必须立即终止使用本软件并销毁本软件产品的任何副本。这项要求对各种拷贝形式有效。 
     
        您同意承担使用本软件产品的风险,在适用法律允许的最大范围内,方倍工作室在任何情况下不就因使用或不能使用本软件产品所发生的特殊的、意外的、非直接或间接的损失承担赔偿责任。即使已事先被告知该损害发生的可能性。 
     
        如使用本软件所添加的任何信息,发生版权纠纷,方倍工作室不承担任何责任。 
     
        方倍工作室对本条款拥有最终解释权。 
     
        CopyRight 2013  方倍工作室  All Rights Reserved 
     
    */ 
    
    define("TOKEN", "weixin");
    
    $wechatObj = new wechatCallbackapiTest();
    if (!isset($_GET['echostr'])) {
        $wechatObj->responseMsg();
    }else{
        $wechatObj->valid();
    }
    
    class wechatCallbackapiTest
    {
        public function valid()
        {
            $echoStr = $_GET["echostr"];
            if($this->checkSignature()){
                echo $echoStr;
                exit;
            }
        }
    
        private function checkSignature()
        {
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
    
            $token = TOKEN;
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
    
            if( $tmpStr == $signature ){
                return true;
            }else{
                return false;
            }
        }
    
        public function responseMsg()
        {
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
            if (!empty($postStr)){
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $RX_TYPE = trim($postObj->MsgType);
    
                switch ($RX_TYPE)
                {
                    case "text":
                        $resultStr = $this->receiveText($postObj);
                        break;
                    case "event":
                        $resultStr = $this->receiveEvent($postObj);
                        break;
                    default:
                        $resultStr = "";
                        break;
                }
                echo $resultStr;
            }else {
                echo "";
                exit;
            }
        }
    
        private function receiveText($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的内容为:".$object->Content;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }
        
        private function receiveEvent($object)
        {
            $contentStr = "";
            switch ($object->Event)
            {
                case "subscribe":
                    $contentStr = "欢迎关注方倍工作室";
                case "unsubscribe":
                    break;
                case "CLICK":
                    switch ($object->EventKey)
                    {
                        case "company":
                            $contentStr[] = array("Title" =>"公司简介", 
                            "Description" =>"方倍工作室提供移动互联网相关的产品及服务", 
                            "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", 
                            "Url" =>"weixin://addfriend/pondbaystudio");
                            break;
                        default:
                            $contentStr[] = array("Title" =>"默认菜单回复", 
                            "Description" =>"您正在使用的是方倍工作室的自定义菜单测试接口", 
                            "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", 
                            "Url" =>"weixin://addfriend/pondbaystudio");
                            break;
                    }
                    break;
                default:
                    break;      
    
            }
            if (is_array($contentStr)){
                $resultStr = $this->transmitNews($object, $contentStr);
            }else{
                $resultStr = $this->transmitText($object, $contentStr);
            }
            return $resultStr;
        }
    
        private function transmitText($object, $content, $funcFlag = 0)
        {
            $textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[%s]]></Content>
    <FuncFlag>%d</FuncFlag>
    </xml>";
            $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $funcFlag);
            return $resultStr;
        }
    
        private function transmitNews($object, $arr_item, $funcFlag = 0)
        {
            //首条标题28字,其他标题39字
            if(!is_array($arr_item))
                return;
    
            $itemTpl = "    <item>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
        </item>
    ";
            $item_str = "";
            foreach ($arr_item as $item)
                $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
    
            $newsTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <Content><![CDATA[]]></Content>
    <ArticleCount>%s</ArticleCount>
    <Articles>
    $item_str</Articles>
    <FuncFlag>%s</FuncFlag>
    </xml>";
    
            $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $funcFlag);
            return $resultStr;
        }
    }
    ?>
    View Code
  • 相关阅读:
    C++
    复盘-2018.6.8~2020.6.8
    C++
    C++
    C++
    C++
    Python学习笔记(十)- 面向对象(一)
    SSHException: Error reading SSH protocol banner
    docker 安装mysql
    docker 安装部署
  • 原文地址:https://www.cnblogs.com/huansky/p/5464828.html
Copyright © 2011-2022 走看看