我们创建一个core的文件夹,里面创建一个Logger.class.php的文件
<?php /** * 日志输出类 */ class Logger { public static function writeTestLog($msg) { self::printLog(‘test‘, $msg); } public static function writeOnlineLog() { } public static function printLog($path, $msg) { $dir = ‘/disk/‘ . $path; //这里的路径是日志保存路径 //判断目录是否存在 不存在则创建之 if (file_exists($dir) === false) { mkdir($dir, 0777); } //Time:xxxIp:xxxMSG:xxx //得到客户端的IP地址 //为了防止别人是通过代理进来的所以要想方法获取到对方的ip,这是一个通用的ip获取方法 if (!empty($_SERVER[‘HTTP_CLIENT_IP‘])) { $ip = $_SERVER[‘HTTP_CLIENT_IP‘]; } else if (!empty($_SERVER[‘HTTP_X_FORWARD_FOR‘])) { $ip = $_SERVER[‘HTTP_X_FORWARD_FOR‘]; } else if (!empty($_SERVER[‘REMOTE_ADDR‘])) { $ip = $_SERVER[‘REMOTE_ADDR‘]; } $filename = $dir . "/" . date(‘Ymd‘) . ".log"; //这里是日志保存在哪 $f = fopen($filename, ‘ab‘); //用追加的方式打开 $content = ‘Time:‘ . date(‘Y-m-d H:i:s‘) . " IP:" . $ip . " MSG:" . $msg; //$content内容是Time:时间戳,IP:xxx,msg是传进的内容 fwrite($f, $content);//写入内容 fclose($f);//写入完成后关闭资源 } }
然后在进行web的根目录里面创建一个index.php
<?php /** * wechat php test */ //包含日志类 require_once ‘core/Logger.class.php‘; //define your token define("TOKEN", "twgdh"); $wechatObj = new wechatCallbackapiTest(); //$wechatObj->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 = "<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"; 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; } } }
我们然后在微信上发个 ‘好’ 字过去, 如果微信上能回复 ‘您好!’ 那恭喜您,您成功了一大半,然后我们再到根目录下看下disk文件夹有没创建成功,如果有了,再看下里面有没有一个test的文件夹里面有一个XXXXXXXXX.log的文件,如果有,那就成功可以看到您刚才创建的日志信息了