Home > Article > Backend Development > Activation of developer mode on WeChat public platform and automatic reply
The content of this article is about the activation and automatic reply of the WeChat public platform developer mode. It has a certain reference value. Now I share it with you. Friends in need can refer to it
First, what is developer mode?
Developer mode is to verify your server address first. After the verification is completed, once the user sends a message to the WeChat official account, WeChat will forward the WeChat user's message to this address. superior. After your server receives the data, you then design a program to output a result, which is then returned to the user by the WeChat server.
## It is recommended to use the test number for personal learning and development
WeChat test account address: http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
After logging in, configure the interface Fill in the information with the URL address and Token just generated.
The URL address is the second-level domain name address. Token is fixed to weixin in the programAfter filling in and submitting, it will prompt that the configuration is successful!
If it prompts "token verification failed", please repeat it several times.
<?php //define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->run(); class wechatCallbackapiTest { public function run(){ if($this->checkSignature() == false){ die("非法请求"); } if(isset($_GET["echostr"])){ $echoStr = $_GET["echostr"]; echo $echoStr; exit; }else{ $this->responseMsg(); } } public function responseMsg(){ //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; // $postStr = file_get_contents("php://input"); file_put_contents('msg.txt',$postStr, FILE_APPEND); //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty($keyword)){ $msgType = "text"; $contentStr = "你好!"; // $contentStr = "hi!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } 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; } } } ?>Related recommendations:
Automatic text reply for PHP WeChat development
Automatic reply for PHP WeChat development
The above is the detailed content of Activation of developer mode on WeChat public platform and automatic reply. For more information, please follow other related articles on the PHP Chinese website!