Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 用php实现微信企业号自定义菜单遇到问题,请大神指点!

用php实现微信企业号自定义菜单遇到问题,请大神指点!

Jun 23, 2016 pm 01:45 PM

有两个文件,一个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);?>
Copy after login



<?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;		}	}}?>
Copy after login


真是非常郁闷,企业号和服务号还是有点不同的。


回复讨论(解决方案)

菜单项不管怎么点,就是没有回复。除了url类型的会正常跳转网页。

看看 菜单的click时间处理了么, 关键词 key对不对 。。。。php文件有没有 语法 错误什么的

http://www.phpjm.net/encode.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles