其一 :接触过微信公众号开发的小伙伴都知道,微信公众号(认证服务号)的后台是给用户提供有自定义菜单设置,并且在那里可以设置菜单并且设置url连接来跳转到网页
其二:我们可以通过微信提供的自定义菜单创建接口,(如何获取AccessToken请参考上一篇文章)
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
//生成自定义菜单(默认菜单)
public function createMenu(){
$access_token = $this->getAccessToken();
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
?
a
c
c
e
s
s
t
o
k
e
n
=
"
.
url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".
url="https://api.weixin.qq.com/cgi−bin/menu/create?accesstoken=".access_token;
//拼装要生成的菜单
$array = array(
'button'=>array(
//第一个一级菜单
// array(
// ‘type’=>‘click’,
// ‘name’=>‘hello’,
// ‘key’ =>‘front’,
//
// ),
array(
“type”=>“view”,
“name”=> ‘world’,
“url”=>“http://www.baidu.com”
),
//第二个一级菜单
// array(
// ‘type’=>‘click’,
// ‘name’ => urlencode($data[‘second_name’]),
// ‘key’ =>‘behind’,
// ),
),
);
//转化成json的格式
$arrayJson = urldecode(json_encode($array));
$this->http_curl($url,'post','json',$arrayJson);
}
CURL请求:
复制代码
public function http_curl(
u
r
l
,
url,
url,type=‘get’,
r
e
s
=
′
j
s
o
n
′
,
res='json',
res=′json′,arr=’’){
//1.初始化
c
h
=
c
u
r
l
i
n
i
t
(
)
;
/
/
2.
设
置
参
数
c
u
r
l
s
e
t
o
p
t
(
ch = curl_init(); //2.设置参数 curl_setopt(
ch=curlinit();//2.设置参数curlsetopt(ch, CURLOPT_URL,
u
r
l
)
;
c
u
r
l
s
e
t
o
p
t
(
url); curl_setopt(
url);curlsetopt(ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if( KaTeX parse error: Expected '}', got 'EOF' at end of input: … curl_setopt(ch , CURLOPT_POST , 1);
curl_setopt($ch , CURLOPT_POSTFIELDS, $arr);
}
//3.执行
r
e
s
u
l
t
=
c
u
r
l
e
x
e
c
(
result = curl_exec(
result=curlexec(ch);
if( curl_errno(KaTeX parse error: Expected '}', got 'EOF' at end of input: …ump(curl_error(ch));
}
if( $res == ‘json’ ){
//将json转化成数组的形式
r
e
s
u
l
t
=
j
s
o
n
d
e
c
o
d
e
(
result = json_decode(
result=jsondecode(result , TRUE);
}
//4.关闭
curl_close($ch);
return $result;
}
复制代码
说明:这里的菜单是定义的二级菜单,定义二级菜单的作用是可以通过其url参数实现点击跳转(特别提示:官方文档中并没有说明在没有一级菜单的情况下不允许定义二级菜单,所以通过自定义接口我们可以直接创建二级菜单)
小插曲:开始按照官方文档指示,并没有创建二级菜单,而是创建一级菜单,然而一级菜单的type事件跟二级的type事件不一样,一级菜单需要开启公众号后台的服务器配置,并且需要配置相关服务器接口,微信根据所配置的接口,将用户操作的内容发送到该接口,一级菜单的click事件绑定的EventKey参数可以用来在接口中识别是点击了哪个自定义菜单,可以根据EventKey的值来进行相关业务逻辑的处理。
其三:创建个性化菜单
根据微信官方文档说明:
个性化菜单匹配规则说明:
个性化菜单的更新是会被覆盖的。 例如公众号先后发布了默认菜单,个性化菜单1,个性化菜单2,个性化菜单3。那么当用户进入公众号页面时,将从个性化菜单3开始匹配,如果个性化菜单3匹配成功,则直接返回个性化菜单3,否则继续尝试匹配个性化菜单2,直到成功匹配到一个菜单。 根据上述匹配规则,为了避免菜单生效时间的混淆,决定不予提供个性化菜单编辑API,开发者需要更新菜单时,需将完整配置重新发布一轮。
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
//创建个性化匹配菜单
public function characterMenu()
{
$access_token = $this->getAccessToken();
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
/
a
d
d
c
o
n
d
i
t
i
o
n
a
l
?
a
c
c
e
s
s
t
o
k
e
n
=
′
.
url='https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token='.
url=′https://api.weixin.qq.com/cgi−bin/menu/addconditional?accesstoken=′.access_token;
$array = array(
'button'=>array(
array(
"type"=>"view",
"name"=> 'hello',
"url"=>"要跳转的url"
),
array(
"type"=>"view",
"name"=> 'world',
"url"=>"要跳转的url"
),
),
"matchrule"=>array(
"tag_id"=>100//标签匹配,微信官方放回给你的标签id
)
);
//转化成json的格式
$arrayJson = urldecode(json_encode($array));
$this->http_curl($url,'post','json',$arrayJson);
}
说明:个性化菜单的创建也是直接创建的二级菜单,如何创建标签id接下来说明(申请标签应在创建个性化菜单之前)
matchrule的匹配规则项我们在这里只使用tag_id标签匹配,其他匹配项请参考微信开放文档(https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Personalized_menu_interface.html)
其四:创建用户标签
创建用户标签也非常简单,直接上代码
复制代码
/**
* 生成用户标签接口
**/
public function createTag()
{
$access_token = $this->getAccessToken();
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
/
t
a
g
s
/
c
r
e
a
t
e
?
a
c
c
e
s
s
t
o
k
e
n
=
′
.
url='https://api.weixin.qq.com/cgi-bin/tags/create?access_token='.
url=′https://api.weixin.qq.com/cgi−bin/tags/create?accesstoken=′.access_token;
$data[‘tag’][‘name’]=urlencode(‘管理员’);
r
e
s
=
res=
res=this->http_curl(
u
r
l
,
′
p
o
s
t
′
,
′
j
s
o
n
′
,
j
s
o
n
e
n
c
o
d
e
(
url,'post','json',json_encode(
url,′post′,′json′,jsonencode(data));
dump($res);
//成功返回的参数包括tag_id
}
复制代码
其五:为用户添加标签
该接口是批量为用户添加标签,参数为数组形式,里面只包含用户在该公众号下的唯一的openid值
复制代码
//批量为用户打上微信公众号的标签类型
public function pasteTag($arr_openid)
{
a
c
c
e
s
s
t
o
k
e
n
=
access_token=
accesstoken=this->getAccessToken();
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
/
t
a
g
s
/
m
e
m
b
e
r
s
/
b
a
t
c
h
t
a
g
g
i
n
g
?
a
c
c
e
s
s
t
o
k
e
n
=
′
.
url='https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token='.
url=′https://api.weixin.qq.com/cgi−bin/tags/members/batchtagging?accesstoken=′.access_token;
d
a
t
a
[
′
o
p
e
n
i
d
l
i
s
t
′
]
=
data['openid_list']=
data[′openidlist′]=arr_openid;
$data[‘tagid’]=你的标签id;
d
a
t
a
=
data=
data=this->http_curl(
u
r
l
,
′
p
o
s
t
′
,
′
j
s
o
n
′
,
j
s
o
n
e
n
c
o
d
e
(
url,'post','json',json_encode(
url,′post′,′json′,jsonencode(data));
dump($data);
}
复制代码
在此,当用户访问该公众号的时候,根据用户身上的标签,会显示不同的菜单栏内容,实现个性化菜单的展示功能
Additional:
为用户取消标签:
openid为数组形式,数组里面是你要取消的该公众号下的唯一的用户的openid
复制代码
//为用户取消标签
public function cancelTag(
o
p
e
n
i
d
,
openid,
openid,tagid)
{
$access_token = $this->getAccessToken();
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
/
t
a
g
s
/
m
e
m
b
e
r
s
/
b
a
t
c
h
u
n
t
a
g
g
i
n
g
?
a
c
c
e
s
s
t
o
k
e
n
=
′
.
url='https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token='.
url=′https://api.weixin.qq.com/cgi−bin/tags/members/batchuntagging?accesstoken=′.access_token;
d
a
t
a
[
′
o
p
e
n
i
d
l
i
s
t
′
]
=
data['openid_list']=
data[′openidlist′]=openid;
d
a
t
a
[
′
t
a
g
i
d
′
]
=
data['tagid']=
data[′tagid′]=tagid;
$res =
t
h
i
s
−
>
h
t
t
p
c
u
r
l
(
this->http_curl(
this−>httpcurl(url,‘post’,‘json’,json_encode(
d
a
t
a
)
)
;
w
x
l
o
g
g
i
n
g
(
′
c
a
n
c
e
l
u
s
e
r
t
a
g
′
,
j
s
o
n
e
n
c
o
d
e
(
data)); wxlogging('cancel_user_tag',json_encode(
data));wxlogging(′cancelusertag′,jsonencode(res));
}
复制代码
获取该公众号创建的标签:
复制代码
//获取微信公众号所创建的标签
public function getTag()
{
a
c
c
e
s
s
t
o
k
e
n
=
access_token=
accesstoken=this->getAccessToken();
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
/
t
a
g
s
/
g
e
t
?
a
c
c
e
s
s
t
o
k
e
n
=
′
.
url='https://api.weixin.qq.com/cgi-bin/tags/get?access_token='.
url=′https://api.weixin.qq.com/cgi−bin/tags/get?accesstoken=′.access_token;
d
a
t
a
=
data=
data=this->http_curl(
u
r
l
)
;
d
u
m
p
(
url); dump(
url);dump(data);
}
复制代码
获取某个标签下的用户:
复制代码
//获取某个标签下面的粉丝openid
public function getFans()
{
a
c
c
e
s
s
t
o
k
e
n
=
access_token=
accesstoken=this->getAccessToken();
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
/
u
s
e
r
/
t
a
g
/
g
e
t
?
a
c
c
e
s
s
t
o
k
e
n
=
′
.
url='https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token='.
url=′https://api.weixin.qq.com/cgi−bin/user/tag/get?accesstoken=′.access_token;
$data[‘tagid’]=100;
d
a
t
a
=
data=
data=this->http_curl(
u
r
l
,
′
p
o
s
t
′
,
′
j
s
o
n
′
,
j
s
o
n
e
n
c
o
d
e
(
url,'post','json',json_encode(
url,′post′,′json′,jsonencode(data));
dump($data);
}
复制代码
删除所有菜单
复制代码
public function delete()
{
a
c
c
e
s
s
t
o
k
e
n
=
access_token=
accesstoken=this->getAccessToken();
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
/
d
e
l
e
t
e
?
a
c
c
e
s
s
t
o
k
e
n
=
′
.
url='https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.
url=′https://api.weixin.qq.com/cgi−bin/menu/delete?accesstoken=′.access_token;
r
e
s
=
res=
res=this->http_curl(
u
r
l
,
′
g
e
t
′
,
′
j
s
o
n
′
)
;
d
u
m
p
(
url,'get','json'); dump(
url,′get′,′json′);dump(res);
}
亚马逊测评 www.yisuping.com