Home > Article > Backend Development > Detailed explanation of the custom reply function of WeChat in PHP version
This article mainly introduces the custom reply function of the PHP version of WeChat, and analyzes the settings and code implementation skills of the PHP version of the WeChat custom reply function in the form of a complete example. Friends in need can refer to the following
for details As follows:
#After configuring the server, you can use php to implement automatic reply.
Code in index.php
<?php define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (isset($_GET['echostr'])) { $wechatObj->valid(); }else{ $wechatObj->responseMsg(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ header('content-type:text'); echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_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($keyword == "?" || $keyword == "?") //获取用户信息 { $msgType = "text"; $contentStr = date("Y-m-d H:i:s",time()); // 回复的内容 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } }else{ echo ""; exit; } } } ?>
Effect:
When the user enters? Or? You will get the current time
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP Introduction to MySQL (database related knowledge)
PHP MySQL operations and methods for reading data
The above is the detailed content of Detailed explanation of the custom reply function of WeChat in PHP version. For more information, please follow other related articles on the PHP Chinese website!