本文共 1959 字,大约阅读时间需要 6 分钟。
配置好服务器之后,就可以用php实现自动回复了。
index.php中的代码
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 = ""; 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; } }}?> %s 0
效果:
当用户输入?或者?就会获取当前时间
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5028754.html,如需转载请自行联系原作者