微信開發——測試號申請,接口配置,JS接口安全域名,自定義菜單
1、申請測試賬號:
先申請公眾號後,點擊進入公從號的管理頁面;找到“開發者工具”,找到“公眾平臺測試賬號”,點擊“進入”。
2、接口配置信息設置
必須要外網哦,也就是微信服務器要能訪問到你填寫到url。
Url到內容有講究,這裏已tp5為例,頁面填寫的Token要跟控制器的Token一致。
接口提交之前的配置項一定要有echostr
設置失敗的一個重要原因:url的請求相應沒有返回echostr這個參數。
接口提交之後在微信裏看效果一定要刪除echostr
<?php namespace app\index\controller; define("TOKEN", "mytest");//聲明token class Jssdkceshi { public function valid() { //接口配置信息修改的時候需要 正式上線不需要 //$echoStr = $_GET["echostr"]; if($this->checkSignature()){ //echo $echoStr; exit; } } public function index(){ $this->valid(); $this->responseMsg();$this->createCaidan(); } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName;$toUsername = $postObj->ToUserName; $ev = $postObj->Event;//獲取事件類型 $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($ev == "subscribe"){ //用戶第一次關註時,自動回復消息設置 $msgType = "text"; $contentStr = "歡迎關註我是微信!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $erer=$keyword; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $erer); echo $resultStr; }else{ echo "Input something..."; } }else { echo ‘‘; 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 gettoken(){ $appid=‘wxa0cb62458ce0a153‘; $secret=‘21fffd4676ee9abf9fcb0b6f6539fab0‘; $getTokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret; $accessTokenJson=file_get_contents($getTokenUrl); $accessTokenArr=json_decode($accessTokenJson,true); $accessToken=$accessTokenArr[‘access_token‘]; return $accessToken; } public function createCaidan(){ $access_token=$this->gettoken(); $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token; $caidan= ‘ { "button":[ { "type":"click", "name":"樂曲欣賞",//一級菜單 "key":"V1001_TODAY_MUSIC" }, { "type":"click", "name":"歌手簡介",//一級菜單 "key":"V1001_TODAY_SINGER" }, { "name":"菜單列表", "sub_button":[ { "type":"view", "name":"搜索",//二級菜單 "url":"http://www.soso.com/" }, { "type":"view", "name":"視頻", "url":"http://v.qq.com/" }, { "type":"view", "name":"友情鏈接", "url":"http://www.baidu.com/" }] }] }‘; $result = $this->https_request($url,$caidan); print_r($result); } //封裝一個函數,使用curl實現post方式的請求 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; } } ?>
弄好後,在頁面填寫url跟token點擊“提交”會有提示配置成功。
3、JS接口安全域名
在公眾號管理頁面,點擊“公眾號設置”,點擊“功能設置”tab頁,找到“JS接口安全域名”,點擊“設置”
在設置頁面中,註意查看設置要求,需要下載MP_verify_EZhQkscUv44pvLNO.txt文件到Web服務器到根目錄下。
設置完畢點擊保存,這樣就JS接口安全域名就設置完畢啦。
4、獲取accesstoken
Accesstoken有有效時間限制的,如果過期啦要重新獲取,而且請求獲取accesstoken的接口調用次數也有限制。所以我們必須要把每次accesstoken的結果保存起來,定時去獲取,官方的說法有效期是兩個小時。
5、創建菜單
在微信開發者文檔中:https://mp.weixin.qq.com/wiki/home/index.html
找到“自定義菜單”下的“自定義菜單創建接口”:
https://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html
註意:
創建前請查看微信的菜單創建定義,只能創建三個一級菜單,二級菜單只能有5個。
具體參數看文檔就知道啦。我們這裏直接使用頁面最下方的工具——“使用網頁調試工具調試該接口”:
https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95&form=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95%E5%88%9B%E5%BB%BA%E6%8E%A5%E5%8F%A3%20/menu/create
access_token:... Body:{ "button": [ { "name": "旅遊導覽", "sub_button": [ { "type": "view", "name": "掃一掃", "url": "http://camp.yfscms.com/wechat/order/parkingPage.wx" }, { "type": "view", "name": "位置服務", "url": "http://camp.yfscms.com/wechat/location/page.wx" }, { "type": "view", "name": "投訴建議", "url": "http://camp.yfscms.com/wechat/order/suggestPage.wx" }, { "type": "view", "name": "民意調查", "url": "http://camp.yfscms.com/wechat/qaList/page.htm" } ] }, { "name": "旅遊服務", "sub_button": [ { "type": "view", "name": "導遊預約", "url": "http://camp.yfscms.com/wechat/guide/list.wx" }, { "type": "view", "name": "車位預定", "url": "http://camp.yfscms.com/wechat/order/parkingPage.wx" }, { "type": "view", "name": "團隊預定", "url": "http://camp.yfscms.com/wechat/order/teamPage.wx" }, { "type": "view", "name": "活動申報", "url": "http://camp.yfscms.com/wechat/order/activityPage.wx" } ] }, { "name": "集中營", "sub_button": [ { "type": "view", "name": "集中營介紹", "url": "http://camp.yfscms.com/wechat/camp/campInfo.wx" }, { "type": "view", "name": "英烈介紹", "url": "http://camp.yfscms.com/wechat/camp/heroList.wx" }, { "type": "view", "name": "聯系我們", "url": "http://camp.yfscms.com/wechat/camp/contact.wx" } ] } ] }
點擊“檢查問題”,如果沒有問題的話,提示:Request successful
如果返回接口中的errcode不等於0,可以查看“全局返回碼說明”——
https://mp.weixin.qq.com/wiki/17/fa4e1434e57290788bde25603fa2fcbd.html
至此菜單創建完畢,可以掃一掃你的測試號,看看菜單是否完成,註意菜單的個數限制問題,微信菜單個數是有限制的!具體看官方的文檔!
微信開發——測試號申請,接口配置,JS接口安全域名,自定義菜單