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;
}
}
}