自定义微信公众号的个性化菜单栏

今天接到一个小任务,要求利用接口来设置不同用户访问微信公众号时显示不同的菜单栏任务,对用户划分人群。第一次接手这样的任务,于是就在网上一顿乱搜,结果搞了半天,浪费了一上午的时间,始终报如下错误:
1
2
3
4

array(2) {
[“errcode”] => int(40001)
[“errmsg”] => string(89) “invalid credential, access_token is invalid or not latest rid: 5faf79a3-00fe0a19-5cca1349”
}

按照报错的提示说,不是有效的access_token或者不是最新的,然后又一直的打印代码查看access_token值,结果并没有发现有不一样的地方。心态崩了~

发现原因:

最后查看官方文档发现,所需要的access_token原来是有两个值,现在使用的这个access_token是这个返回给我的,下面的是一个第三方授权的请求
1
2

KaTeX parse error: Expected '}', got 'EOF' at end of input: …s_token?appid={appid}&secret={KaTeX parse error: Expected 'EOF', got '}' at position 10: appsecret}̲&code={code}&grant_type=authorization_code";
return t h i s − > h t t p G e t F o r J s o n ( this->httpGetForJson( this>httpGetForJson(url);

正确的获取access_token应该是请求下面的url
1
2
3
4

a p p I d = appId= appId=config[‘appid’];
a p p S e c r e t = appSecret= appSecret=config[‘appsecret’];
KaTeX parse error: Expected 'EOF', got '&' at position 75: …ient_credential&̲appid='.appId.’&secret=’.$appSecret;
$arr = t h i s − > h t t p c u r l ( this->http_curl( this>httpcurl(url,‘get’,‘json’);

通关大道:
复制代码

1 public function getAccessToken(){
2 //先获取access_token
3 a p p I d = appId= appId=config[‘appid’];
4 a p p S e c r e t = appSecret= appSecret=config[‘appsecret’];
5 KaTeX parse error: Expected 'EOF', got '&' at position 75: …ient_credential&̲appid='.appId.’&secret=’.$appSecret;
6 $arr = t h i s − > h t t p c u r l ( this->http_curl( this>httpcurl(url,‘get’,‘json’);
7 $access_token = $arr[‘access_token’];
8 return $access_token;
9 }
10
11 //创建菜单
12 public function createMenu(){
13 $access_token = $this->getAccessToken();
14 u r l = " h t t p s : / / a p i . w e i x i n . q q . c o m / c g i − b i n / m e n u / c r e a t e ? 15 a c c e s s t o k e n = " . url = "https://api.weixin.qq.com/cgi-bin/menu/create? 15 access_token=". url="https://api.weixin.qq.com/cgibin/menu/create?15accesstoken=".access_token;
16 $button=array(
17 array(
18 ‘type’=>‘view’,
19 ‘name’=>‘未来科技’,
20 ‘url’=>‘https://www.baidu.com’
21 ),
22 array(
23 ‘name’=>“We*小店”,
24 ‘sub_button’=>array(
25 array(
26 ‘type’=>‘click’,
27 ‘name’=>‘我的订单’,
28 ‘key’=>‘ORDER’
29 ),
30 array(
31 ‘type’=>‘view’,
32 ‘name’=>‘我的中心’,
33 ‘url’=>‘https://www.baidu.com’
34 ),
35 )
36 )
37 );
38 //转化成json的格式
39 a r r a y J s o n = u r l d e c o d e ( j s o n e n c o d e ( arrayJson = urldecode(json_encode( arrayJson=urldecode(jsonencode(array,JSON_UNESCAPED_UNICODE));
40 $res = t h i s − > h t t p c u r l ( this->http_curl( this>httpcurl(url,‘post’,‘json’, a r r a y J s o n ) ; 41 d u m p ( arrayJson); 41 dump( arrayJson);41dump(res);
42 }

复制代码

成功提示:
1
2
3
4

array(2) {
[“errcode”] => int(0)
[“errmsg”] => string(2) “ok”
}

tip小陷阱:

1.烦人的报错代码:

解决方案:检查菜单栏设置的url是否是有效的url(已备案且能正常访问的链接)
1
2
3
4

array(2) {
[“errcode”] => int(40054)
[“errmsg”] => string(61) “invalid sub button url domain rid: 5faf808f-4e6897ec-0bf2ff16”
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值