PHP 监听微信菜单是否被点击,PHP微信公众平台自定义菜单通过key值点击响应

PHP微信公众平台自定义菜单响应事件,想知道如何才能通过key值点击响应,下面的源码如下

;header("Content-type: text/html; charset=utf-8");

header("Content-type: text/html; charset=gb2312");

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx80707459238738c4&secret=4cb8dc6a99889cbb3a9e8c3287e3cfa9";

$content = file_get_contents($url);

$info = json_decode($content);

print_r($info);

echo $info->access_token;

$ACCESS_TOKEN=$info->access_token;

$data = '{

"button":[

{

"type":"click",

"name":"Find",

"key":"find"

},

{

"type":"click",

"name":"Check",

"key":"lock_acount"

},

{

"type":"click",

"name":"About",

"key":"other"

}]

}';

$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)) {

echo 'Errno'.curl_error($ch);

}

curl_close($ch);

var_dump($tmpInfo);

?>

这里是获取微信返回的数据

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//返回回复数据

if (!empty($postStr))

{

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$this->fromUsername = $postObj->FromUserName;//发送消息方ID

$this->toUsername = $postObj->ToUserName;//接收消息方ID

$this->keyword = trim($postObj->Content);//用户发送的消息

$this->times = time();//发送时间

$MsgType = $postObj->MsgType;//消息类型

if($MsgType=='event')  //判断微信自定义响应事件

{

$MsgEvent = $postObj->Event;//获取事件类型

if ($MsgEvent=='subscribe'){  //订阅事件

#要返回的消息

}elseif ($MsgEvent=='CLICK'){  //点击菜单

//点击事件

$EventKey = $postObj->EventKey;//菜单的自定义的key值,可以根据此值判断用户点击了什么内容,从而推送不同信息

switch($EventKey){

case "find" :  //菜单中key相关值

#返回的消息

break;

case "lock_acount":

#返回的消息

break;

case "other":

#返回的消息

break;

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值