微信公众号开发自定义菜单

E等于MC平方
领域专家: 后端开发技术领域
2016-09-09 05:08:55
用的是github上开源的wechat.class.php sdk。index.php文件,也是我在公众号服务器配置的URL地址,index.php代码如下:


<?php

require_once "wechat.class.php";


$options = array(
'token'=>'123456', //填写你设定的key
'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey,如接口为明文模式可忽略
'appid'=>'xxxxxxx', //appid
'appsecret'=>'xxxxxxx',
'debug'=>true,
'logcallback'=>'user_dump' //日志文件
);


$weObj = new Wechat($options);
$ret = $weObj->valid();//明文或兼容模式可以在接口验证通过后注释此句,但加密模式一定不能注释,否则会验证失败
if (!$ret) {
user_dump("验证失败");
//exit;
}

//设置菜单
$newmenu = array (
'button' => array (
0 => array (
'name' => '我是商户',
'sub_button' => array (
0 => array (
'type' => 'view',
'name' => '绑定',
'url' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx219d0736743e4699&redirect_uri=http://g1t5549946.imwork.net/inn_wechat_nofity/bind.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect',
),
1 => array (
'type' => 'view',
'name' => '解绑',
'url' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx219d0736743e4699&redirect_uri=http://g1t5549946.imwork.net/inn_wechat_nofity/unbind.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect',
),
2 => array (
'type' => 'view',
'name' => '订单查询',
'url' => 'http://g1t5549946.imwork.net/inn_wechat_nofity/order_query.php',
),
),
),
)
);

$result = $weObj->createMenu($newmenu);


$type = $weObj->getRev()->getRevType();
$events = $weObj->getRevEvent();
switch($type) {
case Wechat::MSGTYPE_TEXT:
$weObj->text("hello, I'm wechat")->reply();
exit;
break;
case Wechat::MSGTYPE_IMAGE:
break;
case Wechat::MSGTYPE_EVENT:
if($events['event'] == Wechat::EVENT_SUBSCRIBE)//首次关注
{

}
elseif ($events['event'] == Wechat::EVENT_UNSUBSCRIBE)
{
//取消关注
}
break;
default:
$weObj->text("help info")->reply();
break;
}



我的问题是,这个createmenu方法可以创建一个自定义的菜单,但是index.php这个页面会被经常调用(比如关注着给公众号发消息),总不能每次都去调用createmenu吧? 菜单只要一次创建成功了,后面就不用不断的调用了吧?如何在代码中控制呢?

希望我说明白了。
...全文
522 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
木凡007 2016-09-14
  • 打赏
  • 举报
回复
调一次 然后就不在调用不就完了----@V型知识库
E等于MC平方 2016-09-12
  • 打赏
  • 举报
回复
引用 2 楼 qq840727854 的回复:
把设置的 内容存一下 下次请求了直接返回~~
不太明白,能来个demo吗
E等于MC平方 2016-09-12
  • 打赏
  • 举报
回复
1楼是啥意思?封装成类后,createmenu调用一次后,注释掉?
qq840727854 2016-09-12
  • 打赏
  • 举报
回复
把设置的 内容存一下 下次请求了直接返回~~
dnpao 2016-09-12
  • 打赏
  • 举报
回复
<?php
require_once "wechat.class.php";

class wx{
	
	public $weObj=NULL;	
	
	public function __construct(){
		$options = array(
			'token'=>'123456', //填写你设定的key
			'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey,如接口为明文模式可忽略
			'appid'=>'xxxxxxx', //appid
			'appsecret'=>'xxxxxxx',
			'debug'=>true,
			'logcallback'=>'user_dump' //日志文件
		);
		
		$this->weObj = new Wechat($options);
		$ret = $this->weObj->valid();//明文或兼容模式可以在接口验证通过后注释此句,但加密模式一定不能注释,否则会验证失败
		if (!$ret) {
			user_dump("验证失败");
			//exit;
		}	
	}
	
	public function index(){
		$type = $this->weObj->getRev()->getRevType();
		$events = $this->weObj->getRevEvent();
		switch($type) {
			case Wechat::MSGTYPE_TEXT:
				$this->weObj->text("hello, I'm wechat")->reply();
				exit;
				break;
			case Wechat::MSGTYPE_IMAGE:
				break;
			case Wechat::MSGTYPE_EVENT:
				if($events['event'] == Wechat::EVENT_SUBSCRIBE)//首次关注
				{
					
				}
				elseif ($events['event'] == Wechat::EVENT_UNSUBSCRIBE)
				{
					//取消关注
				}
				break;
			default:
				$this->weObj->text("help info")->reply();
				break;
		}
	}
	
	
	//创建菜单
	public function createMenu(){
		$newmenu =  array (
			'button' => array (
				0 => array (
					'name' => '我是商户',
					'sub_button' => array (
						0 => array (
							'type' => 'view',
							'name' => '绑定',
							'url' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx219d0736743e4699&redirect_uri=http://g1t5549946.imwork.net/inn_wechat_nofity/bind.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect',
						),
						1 => array (
							'type' => 'view',
							'name' => '解绑',
							'url' => 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx219d0736743e4699&redirect_uri=http://g1t5549946.imwork.net/inn_wechat_nofity/unbind.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect',
						),
						2 => array (
							'type' => 'view',
							'name' => '订单查询',
							'url' => 'http://g1t5549946.imwork.net/inn_wechat_nofity/order_query.php',
						),
					),
				),
			)
		);
	
		$result = $this->weObj->createMenu($newmenu);
		return $result;
	}
}


$wx=new wx();
echo $wx->index(); //调用index
//echo $wx->createMenu(); //创建菜单

3,159

社区成员

发帖
与我相关
我的任务
社区描述
微信开发即微信公众平台开发,将企业信息、服务、活动等内容通过微信网页的方式进行表现,通过二次开发可以将公众账号由一个媒体型营销工具转化成提供服务的产品。
社区管理员
  • 微信开发
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧