<?php header(‘Content-type:text‘); define("TOKEN", "weixin"); define("AppID", "wx0016e715caddff6f"); define("EncodingAESKey", "mKbYrK5PDc3tV8td9LqLVVU0hErwI2Bsq83ZRYqdp2S"); include_once "wxBizMsgCrypt.php"; $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET[‘echostr‘])) { $wechatObj->responseMsg(); } else { $wechatObj->valid(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if ($tmpStr == $signature) { echo $echoStr; exit; } } public function responseMsg() { $timestamp = $_GET[‘timestamp‘]; $nonce = $_GET["nonce"]; $msg_signature = $_GET[‘msg_signature‘]; $encrypt_type = "aes"; $postStr = file_get_contents(‘php://input‘); if (!empty($postStr)) { //解密 if ($encrypt_type == ‘aes‘){ $pc = new WXBizMsgCrypt(TOKEN, EncodingAESKey, AppID); // $this->logger(" D \r\n".$postStr); $decryptMsg = ""; //解密后的明文 $errCode = $pc->DecryptMsg($msg_signature, $timestamp, $nonce, $postStr, $decryptMsg); // var_dump($errCode);exit; $postStr = $decryptMsg; } $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; } // $this->logger("T " . $result); //加密 if ($encrypt_type == ‘aes‘){ $encryptMsg = ‘‘; //加密后的密文 $errCode = $pc->encryptMsg($result, $timestamp, $nonce, $encryptMsg); $result = $encryptMsg; // $this->logger(" E \r\n".$result); } echo $result; } else { echo ""; exit; } } private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "关注了就好加油哦"; break; case "unsubscribe": $content = "取消关注"; break; } $result = $this->transmitText($object, $content); return $result; } //接收文本消息 private function receiveText($object) { $keyword = trim($object->Content); $content = $object->ToUserName . "\n".$object->FromUserName. "\n".$object->Content. "\n".$object->MsgType; if (is_array($content)) { if (isset($content[0][‘PicUrl‘])) { $result = $this->transmitNews($object, $content); } else if (isset($content[‘MusicUrl‘])) { $result = $this->transmitMusic($object, $content); } } else { $result = $this->transmitText($object, $content); } return $result; } private function transmitText($object, $content) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } private function transmitNews($object, $arr_item) { if (!is_array($arr_item)) return; $itemTpl = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]); $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> </xml>"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item)); return $result; } private function transmitMusic($object, $musicArray) { $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray[‘Title‘], $musicArray[‘Description‘], $musicArray[‘MusicUrl‘], $musicArray[‘HQMusicUrl‘]); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } private function logger($log_content) { if (isset($_SERVER[‘HTTP_APPNAME‘])) { //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); } else if ($_SERVER[‘REMOTE_ADDR‘] != "127.0.0.1") { //LOCAL $max_size = 10000; $log_filename = "log.xml"; if (file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)) { unlink($log_filename); } file_put_contents($log_filename, date(‘H:i:s‘) . " " . $log_content . "\r\n", FILE_APPEND); } } } ?>