夺命雷公狗---微信开发02----了解下微信公众平台
我们创建一个core的文件夹,里面创建一个Logger.class.php的文件
然后在进行web的根目录里面创建一个index.php
valid(); $wechatObj->responseMsg(); Logger::writeTestLog(json_encode($_GET)); class 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"]; Logger::writeTestLog("postStr" . $postStr); //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = ""; if(!empty( $keyword )) { $msgType = "text"; if ($keyword == '你好' or $keyword == '您好' or $keyword == 'hello' or $keyword == '好') { $contentStr = '您好!'; } else if ($keyword == '北京') { $contentStr = '北京是中华人民共和国的首都!'; } else if (preg_match('/^.*天气.*$/', $keyword)) { $contentStr = "北京今天天气不错,我们下午没有课!"; } else { $contentStr = "感谢您对双哥PHP的关注与支持!"; //默认的回复 } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } %s 0
我们然后在微信上发个 ‘好’ 字过去, 如果微信上能回复 ‘您好!’ 那恭喜您,您成功了一大半,然后我们再到根目录下看下disk文件夹有没创建成功,如果有了,再看下里面有没有一个test的文件夹里面有一个XXXXXXXXX.log的文件,如果有,那就成功可以看到您刚才创建的日志信息了