Heim > Artikel > Backend-Entwicklung > 用php实现微信企业号自定义菜单遇到问题,请大神指点!
有两个文件,一个response.php是用来响应消息的。
另一个是初始页面Sample.php。问题在于,我菜单创建出来了,但是点击之后没有出现我response里面的消息回发,不知道为什么。
<?phpinclude_once "Response.php";define("CORPID","wx5aae8fe610db907c");define("CORPSECRET","zmEbFeMuTwVJlUiZdv2-502ULoM0qJX3RUrXywMXnC_rqZgGuJIX3MUAnwWBQ6cQ");$token_access_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=".CORPID."&corpsecret=".CORPSECRET;$wechatObj = new wechatCallbackapiTest();$wechatObj->responseMsg();$res = file_get_contents($token_access_url);$arr_result = json_decode($res,true);define("ACCESS_TOKEN",$arr_result['access_token']);$make_menu_url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token=".ACCESS_TOKEN."&agentid=2";$menudata = '{ "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "type":"click", "name":"歌手简介", "key":"V1001_TODAY_SINGER" }, { "name":"菜单", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.soso.com/" }, { "type":"view", "name":"视频", "url":"http://v.qq.com/" }, { "type":"click", "name":"赞一下我们", "key" :"V1001_GOOD" }] }] }'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $make_menu_url); 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, $menudata); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); $info = curl_exec($ch); if(curl_errno($ch)){ echo 'Errno'.curl_error($ch); } curl_close($ch); print_r($info);?>
<?phpclass wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; $sendmsg = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".ACCESS_TOKEN; //extract post data $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $MsgType = $postObj->MsgType; $MsgId = $postObj->MsgId; $Event = $postObj->Event; $EventKey = $postObj->EventKey; $CreateTime = intval($postObj->CreateTime); $formTime = date("Y-m-d H:i:s",$CreateTime); $agentid = $postObj->AgentID; $msg = "开发者id:".$toUsername."\n"; $msg .= "用户id:".$fromUsername."\n"; $msg .= "事件消息id:".$MsgId."\n"; $msg .= "事件消息类型id:".$MsgType."\n"; $msg .= "事件类型:".$Event."\n"; $msg .= "事件KEY值,与自定义菜单接口中KEY值对应:".$EventKey."\n"; $msg .= "事件发送过来的时间戳:".$CreateTime."\n"; $msg .= "消息发生具体时间:".$formTime."\n"; /* $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; */ $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Event><![CDATA[%s]]></Event> <EventKey><![CDATA[%s]]></EventKey> <AgentID>%s</AgentID> </xml>"; $contentStr = $msg; $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$formTime,$MsgType,$Event,$EventKey, $agentid); echo $resultStr; } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>
菜单项不管怎么点,就是没有回复。除了url类型的会正常跳转网页。
看看 菜单的click时间处理了么, 关键词 key对不对 。。。。php文件有没有 语法 错误什么的
http://www.phpjm.net/encode.html