valid(); $this->parseMp(); } public function valid() { if ($this->checkSignature()) { $echoStr = $_GET['echostr']; if ($echoStr != "") { echo $echoStr; exit(); } } else { exit("非法访问1!"); } } public function parseMp() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (! empty($postStr)) { libxml_disable_entity_loader(true); $this->mpobj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); } else { exit("非法访问2"); } } /** * 获取用户发过来的指令keyword */ public function getKeyword() { return InitPHP::output($this->mpobj->Content); } /** * 获取消息类型 */ public function getType() { return $this->mpobj->MsgType; } /** * 回复文本消息 * * @param string $msg * 消息内容 */ public function responseText($msg) { $textTpl = " %s 0 "; $resultStr = sprintf($textTpl, $this->mpobj->FromUserName, $this->mpobj->ToUserName, time(), "text", $msg); echo $resultStr; die(); } /** * 校验授权 * * @throws TOKEN未定义异常 * @return boolean */ private function checkSignature() { if (! defined("TOKEN")) { throw new Exception('TOKEN未定义!'); } $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; } } }