开发自定义菜单 以及外链URL

来源:互联网 发布:php前端教程视频 编辑:程序博客网 时间:2024/06/16 11:21

由于自定义菜单里面放URL需要认证企业号,所以 我选择了创建测试账号。

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login登录创建测试号,

(重点)需要填写自己服务器的url,token,这个 以及signature验证加密签名。

 

Appid  secrect获取access_token


Access_token是用来操作 自定义菜单的 密匙。

返回数据:


然后 在调试页面http://mp.weixin.qq.com/debug

 就可以进行调试了(也可以自己写代码 进行post



下面是自己发送的json数据包

{    "button": [        {            "name": "天气预报",             "sub_button": [                {                    "type": "click",                     "name": "北京天气",                     "key": "天气北京"                },                 {                    "type": "click",                     "name": "上海天气",                     "key": "天气上海"                },                 {                    "type": "click",                     "name": "广州天气",                     "key": "天气广州"                },                 {                    "type": "click",                     "name": "深圳天气",                     "key": "天气深圳"                },                 {                    "type": "view",                     "name": "本地天气",                     "url": "http://m.hao123.com/a/tianqi"                }            ]        },         {            "name": "涵涵工作室",             "sub_button": [                {                    "type": "click",                     "name": "公司简介",                     "key": "company"                },                 {                    "type": "click",                     "name": "趣味游戏",                     "key": "游戏"                },                 {                    "type": "click",                     "name": "讲个笑话",                     "key": "笑话"                }            ]        }    ]}

下面是 服务器上的代码:

<?php/**  * wechat php test  *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();/*******如果是第一次使用接口 就需要 返回 echostr参数*******/if(isset($_GET['echostr'])){$wechatObj->valid();}else{$wechatObj->responseMsg();}class wechatCallbackapiTest{public function valid()    {        $echoStr = $_GET["echostr"];        //valid signature , option        if($this->checkSignature()){        echo $echoStr;        exit;        }    }    public function responseMsg()    {//get post data, May be due to the different environments$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];      //extract post dataif (!empty($postStr)){                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,                   the best way is to check the validity of xml by yourself */             //   libxml_disable_entity_loader(true);              $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);                $fromUsername = $postObj->FromUserName;                $toUsername = $postObj->ToUserName;                $keyword = trim($postObj->Content);                $time = time();                $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>";             if(!empty( $keyword ))                {              $msgType = "text";                $contentStr = "Welcome to wechat world!";                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                echo $resultStr;                }        }else {        echo "";        exit;        }    }/*****************************验证消息的确来自微信服务器: signature微信加密签名 结合开发者填写的token参数和情趣中的timestamp时间戳,noce参数 nonce随机数 echostr随机字符串 开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器 请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失效。加密/校验流程: 1.将token ,timestamp,nonce三个参数进行字典序排序 2.将三个参数字符串拼成一个字符串进行sha1加密 3.开发者获得加密后的字符串可与signature对比,标识该请求来自微信。********************************/private function checkSignature(){        // you must define TOKEN by yourself        if (!defined("TOKEN")) {            throw new Exception('TOKEN is not defined!');        }                $signature = $_GET["signature"];        $timestamp = $_GET["timestamp"];        $nonce = $_GET["nonce"];        $token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);        // use SORT_STRING rule  字典序排序sort($tmpArr, SORT_STRING);    //|拼接$tmpStr = implode( $tmpArr );//|sha1加密$tmpStr = sha1( $tmpStr );//|判断是否相等if( $tmpStr == $signature ){return true;}else{return false;}}}?>


加一个url在里面:

{    "button": [        {            "name": "天气预报",             "sub_button": [                {                    "type": "click",                     "name": "北京天气",                     "key": "天气北京"                },                 {                    "type": "click",                     "name": "上海天气",                     "key": "天气上海"                },                 {                    "type": "click",                     "name": "广州天气",                     "key": "天气广州"                },                 {                    "type": "click",                     "name": "深圳天气",                     "key": "天气深圳"                },                 {                    "type": "view",                     "name": "本地天气",                     "url": "http://m.hao123.com/a/tianqi"                }            ]        },         {            "name": "涵涵工作室",             "sub_button": [                {                    "type": "click",                     "name": "公司简介",                     "key": "company"                },                 {                    "type": "click",                     "name": "趣味游戏",                     "key": "游戏"                },                 {                    "type": "click",                     "name": "讲个笑话",                     "key": "笑话"                },                 {                    "type": "view",                     "name": "链接到百度",                     "url": "http://baidu.com"                }            ]        }    ]}



0 0
原创粉丝点击