开发——测试号申请,接⼝配置,JS接⼝安全域名,⾃定义菜单
1、申请测试账号:
先申请后,点击进⼊公从号的管理页⾯;到“开发者⼯具”,到“公众平台测试账号”,点击“进⼊”。
2、接⼝配置信息设置
必须要外⽹哦,也就是服务器要能访问到你填写到url。
Url到内容有讲究,这⾥已tp5为例,页⾯填写的Token要跟控制器的Token⼀致。
设置失败的⼀个重要原因:url的请求相应没有返回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!";
$contentStr = "Welcome to wechat world!";
$erer=$keyword;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $erer);
echo$resultStr;
}else{
echo "";
}
}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="api.weixin.qq/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="api.weixin.qq/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":"www.soso/"
},
{
"type":"view",
"name":"视频",
"url":"v.qq/"
},
{
"type":"view",
"name":"友情链接",
"url":"www.baidu/"
}]
}]
}';
$result = $this->https_request($url,$caidan);
print_r($result);
}
//封装⼀个函数,使⽤curl实现post⽅式的请求
//封装⼀个函数,使⽤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_⽂件到Web服务器到根⽬录下。
设置完毕点击保存,这样就JS接⼝安全域名就设置完毕啦。
4、获取accesstoken
Accesstoken有有效时间限制的,如果过期啦要重新获取,⽽且请求获取accesstoken的接⼝调⽤次数也有限制。所以我们必须要把每次accesstoken的结果保存起来,定时去获取,官⽅的说法有效期是两个⼩时。
5、创建菜单
到“⾃定义菜单”下的“⾃定义菜单创建接⼝”:
注意:
创建前请查看的菜单创建定义,只能创建三个⼀级菜单,⼆级菜单只能有5个。
具体参数看⽂档就知道啦。我们这⾥直接使⽤页⾯最下⽅的⼯具——“使⽤⽹页调试⼯具调试该接⼝”:
access_token:...
Body:{
"button": [
{
"name": "旅游导览",
"sub_button": [
{
"type": "view",
"name": "扫⼀扫",
"url": "camp.yfscms/wechat/order/parkingPage.wx"
},
{
"type": "view",
"name": "位置服务",
"url": "camp.yfscms/wechat/location/page.wx"
},
{
"type": "view",
"name": "投诉建议",
"url": "camp.yfscms/wechat/order/suggestPage.wx"
},
{
{
"type": "view",
"name": "民意调查",
"url": "camp.yfscms/wechat/qaList/page.htm"
}
]
},
{
蜗居主要演员"name": "旅游服务",
"sub_button": [
{
"type": "view",
"name": "导游预约",
"url": "camp.yfscms/wechat/guide/list.wx"
},
{
"type": "view",
"name": "车位预定",
"url": "camp.yfscms/wechat/order/parkingPage.wx"
},
{
"type": "view",
"name": "团队预定",
"url": "camp.yfscms/wechat/order/teamPage.wx"
净化水},
{
"type": "view",
"name": "活动申报",
"url": "camp.yfscms/wechat/order/activityPage.wx"
}
]
},
{
"name": "集中营",
"sub_button": [
{
"type": "view",
"name": "集中营介绍",
"url": "camp.yfscms/wechat/camp/campInfo.wx"
},
{
"type": "view",
"name": "英烈介绍",
"url": "camp.yfscms/wechat/camp/heroList.wx"
},
{
"type": "view",
"name": "",
"url": "camp.yfscms/wechat/camp/contact.wx"
}
]
}
]
}
点击“检查问题”,如果没有问题的话,提⽰:Request successful
如果返回接⼝中的errcode不等于0,可以查看“全局返回码说明”——
⾄此菜单创建完毕,可以扫⼀扫你的测试号,看看菜单是否完成,注意菜单的个数限制问题,菜单个数是有限制的!具体看官⽅的⽂档!
发布评论