远程即时修改微信菜单

场景:

某天程序员A加班加点完成了本次微信项目的迭代工作,在使用全身解数之后终于盼来了上线的等待,当然,后面也是成功上线了,但是项目经理在看过线上项目之后,才发现程序员A在微信后台修改的操作菜单并没有生效,当然程序员A也知道,这个生效要等一小时左右,但是项目经理急了,’我马上要给客户看,怎么能等待生效’。

这是一个线上执行的单文件,如有需要拆分成各个板块形成方法,可以自行去改。
直接执行以下代码能即时修改微信菜单,比微信后台修改操作快很多。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
$appid = "xxxxxxx";          //微信appid
$appsecret = "xxxxxxxxxxx"; //微信secret_id
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$host = "http://xxxxxx.com"; //站点域名
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];

header("Content-type: text/html; charset=utf-8");
//创建菜单
function createMenu($data, $access_token){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
//获取菜单
function getMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN);
}

//删除菜单
function deleteMenu(){
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".ACCESS_TOKEN);
}

$data = array(
"button" => array(
array(
"name"=>urlencode("推广赚钱"),
"sub_button"=>array(
array(
"type"=>"view",
"name"=>urlencode("首页"),
"url"=>$host."/default/index?needLogin=1&agent_r=agent_wx"
),
array(
"type"=>"view",
"name"=>urlencode("做任务"),
"url"=>$host."/proxy/taskHall?agent_r=agent_wx"
),
array(
"type"=>"view",
"name"=>urlencode("提商机"),
"url"=>$host."/business/add?agent_r=agent_wx"
),
array(
"type"=>"view",
"name"=>urlencode("推广爆品"),
"url"=>$host."/proxy/home?agent_r=agent_wx"
),array(
"type"=>"view",
"name"=>urlencode("推广店铺"),
"url"=>$host."/rebateshop/index?agent_r=agent_wx"
)
)
),
array(
"name"=>urlencode("发展下级"),
"sub_button"=>array(
array(
"type"=>"view",
"name"=>urlencode("发展下级"),
"url"=>$host."/invite/newinviteentershow?agent_r=agent_wx"
),
array(
"type"=>"view",
"name"=>urlencode("管理下级"),
"url"=>$host."/partner/mypartner?agent_r=agent_wx"
)
)
),
array(
"name"=>urlencode("我的"),
"sub_button"=>array(
array(
"type"=>"view",
"name"=>urlencode("收益"),
"url"=>$host."/earnings/BalanceCommission?agent_r=agent_wx"
),
array(
"type"=>"view",
"name"=>urlencode("我的店铺"),
"url"=>$host."/shop/detail?agent_r=agent_wx"
),
array(
"type"=>"view",
"name"=>urlencode("个人中心"),
"url"=>$host."/home/ucenter?agent_r=agent_wx"
)

)
)
)
);
//转义汉字
$data = json_encode($data);
$data = urldecode($data);
echo $data;echo "<br>\n";
echo createMenu($data, $access_token);echo "\n";
谢翔 wechat
坚持原创技术分享,您的支持将鼓励我继续创作!
-------------本文结束感谢您的阅读-------------
0%