微信开发者平台自定义接口一.主接口文件mcinterface.Php<?php/*** wechat php test*/// define your tokendefine("TOKEN", "******");include_once ("weixin.class.php");include_once ("search.php");$weixin = new Weixin(TOKEN);$chaxun = new chaxun();if (!isset($_GET['echostr'])) {$weixin->getMsg();} else {$weixin->valid();}$type = $weixin->msgtype;$username = $weixin->msg['FromUserName'];switch ($type) {case "event": // 事件判断$results = $chaxun->handleEvent($weixin);break;case "text": // 用户文本$keyword = $weixin->msg['Content'];$results = $chaxun->usrMsg($keyword, $username);// $results=$chaxun->search($keyword);break;case "image":$keyword = "image";$results = $chaxun->help($keyword);break;case "location":case "voice":case "shortvideo":$keyword = "video";$results = $chaxun->help($keyword);break;default:$results = "Unknow Event:" . $type;break;}if (is_array($results)) {switch ($results["msgType"]){case "news":$reply = $weixin->makeNews($results);break;case "music":$reply = $weixin->makeMusic($results);break;default:$reply = $weixin->makeNews($results);break;}} else {$reply = $weixin->makeText($results);}$weixin->reply($reply);?>二.信息处理函数(weixin.class.php)<?PHPrequire_once ('wxBizMsgCrypt.php');define("TOKEN", "***");define("AppID", "***");define("EncodingAESKey", "***");define("debug", "false");class Weixin{public $token = "***";public $debug = false;public $setFlag = false;public $msg = array();public function getMsg(){$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if ($this->debug) {$this->write_log($postStr);}// 获取加密解密参数if (! empty($postStr)) {$postStr = $this->msgDecrypt($postStr);$this->msg = (array) simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDA TA);$this->msgtype = strtolower($this->msg['MsgType']);}}public function makeText($text = ''){$CreateTime = time();$FuncFlag = $this->setFlag ? 1 : 0;$textTpl = "<xml><ToUserName><![CDA TA[{$this->msg['FromUserName']}]]></ToUserName><FromUserName><![CDA TA[{$this->msg['ToUserName']}]]></FromUserName><CreateTime>{$CreateTime}</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>%s</FuncFlag></xml>";return sprintf($textTpl, $text, $FuncFlag);}public function makeVideo($newsData = array()){$CreateTime = time();$FuncFlag = $this->setFlag ? 1 : 0;$textTpl = "<xml><ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName><FromUserName><![CDA TA[{$this->msg['ToUserName']}]]></FromUserName><CreateTime>{$CreateTime}</CreateTime><MsgType><![CDA TA[video]]></MsgType><Video><MediaId><![CDA TA[$s]]></MediaId><Title><![CDA TA[%s]]></Title><Description><![CDATA[%s]]></Description></Video></xml>";$Videostr=sprintf($textTpl,$newsData["MediaId"],$newsData["Title"],$newsData["Descripti on"]);return $Videostr;}public function makeMusic($newsData = array()){$CreateTime = time();$FuncFlag = $this->setFlag ? 1 : 0;$textTpl = "<xml><ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName><FromUserName><![CDA TA[{$this->msg['ToUserName']}]]></FromUserName><CreateTime>{$CreateTime}</CreateTime><MsgType><![CDA TA[music]]></MsgType><Music><Title><![CDA TA[%s]]></Title><Description><![CDATA[%s]]></Description><MusicUrl><![CDA TA[%s]]></MusicUrl><HQMusicUrl><![CDATA[%s]]></HQMusicUrl><ThumbMediaId><![CDA TA[%s]]></ThumbMediaId></Music></xml>";$Musicstr=sprintf($textTpl,$newsData["Title"],$newsData["Description"],$newsData["MusicUrl"],$newsData["HQMusicUrl "],$newsData["ThumbMediaId"]);return $Musicstr;}public function makeScancode($text = ''){$CreateTime = time();$FuncFlag = $this->setFlag ? 1 : 0;$textTpl = "<xml><ToUserName><![CDA TA[{$this->msg['FromUserName']}]]></ToUserName><FromUserName><![CDA TA[{$this->msg['ToUserName']}]]></FromUserName><CreateTime>{$CreateTime}</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDA TA[scancode_waitmsg]]></Event><EventKey><![CDATA[{$this->msg['EventKey']}]]></EventKey><ScanCodeInfo><ScanType><![CDA TA[qrcode]]></ScanType><ScanResult><![CDATA[%s]]></ScanResult></ScanCodeInfo></xml>";return sprintf($textTpl, $text, $FuncFlag);}public function makeCustomer($text = ''){$CreateTime = time();$textTpl = "<xml><ToUserName><![CDA TA[{$this->msg['FromUserName']}]]></ToUserName><FromUserName><![CDA TA[{$this->msg['ToUserName']}]]></FromUserName><CreateTime>{$CreateTime}</CreateTime><MsgType><![CDATA[transfer_customer_service]]></MsgType></xml>";return sprintf($textTpl);}public function makeNews($newsData = array()){$CreateTime = time();$FuncFlag = $this->setFlag ? 1 : 0;$newTplHeader = "<xml><ToUserName><![CDA TA[{$this->msg['FromUserName']}]]></ToUserName><FromUserName><![CDA TA[{$this->msg['ToUserName']}]]></FromUserName><CreateTime>{$CreateTime}</CreateTime><MsgType><![CDATA[news]]></MsgType><Content><![CDATA[%s]]></Content><ArticleCount>%s</ArticleCount><Articles>";$newTplItem = "<item><Title><![CDATA[%s]]></Title><Description><![CDA TA[%s]]></Description><PicUrl><![CDA TA[%s]]></PicUrl><Url><![CDA TA[%s]]></Url></item>";$newTplFoot = "</Articles></xml>";$Content = '';$itemsCount = count($newsData);$itemsCount = $itemsCount < 10 ? $itemsCount : 10;if ($itemsCount) {foreach ($newsData as $key => $item) {if ($key <= 9) {$Content .= sprintf($newTplItem, $item['title'], $item['description'], $item['picurl'], $item['url']);}}}$header = sprintf($newTplHeader, $newsData['content'], $itemsCount);$footer = sprintf($newTplFoot, $FuncFlag);$reString = $header . $Content . $footer;return $reString;}public function reply($data){if ($this->debug) {$this->write_log($data);}$data = $this->msgEncrypt($data);echo $data;}public function valid(){$echoStr = $_GET["echostr"];// valid signature , optionif ($this->checkSignature()) {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 msgDecrypt($postStr){$arry = $this->getPost();if ($arry["encrypt_type"] == 'AES' or $arry["encrypt_type"] == 'aes') {$pc = new WXBizMsgCrypt(TOKEN, EncodingAESKey, AppID);$decryptMsg = ""; // 解密后的明文$errCode = $pc->decryptMsg($arry["msg_signature"], $arry["timestamp"], $arry["nonce"], $postStr, $decryptMsg);$postStr = $decryptMsg;}return $postStr;}public function msgEncrypt($data){$arry = $this->getPost();if ($arry["encrypt_type"] == 'AES' or $arry["encrypt_type"] == 'aes') {$pc = new WXBizMsgCrypt(TOKEN, EncodingAESKey, AppID);$encryptMsg = ''; // 加密后的密文$errCode = $pc->encryptMsg($data, $arry["timestamp"], $arry["nonce"], $encryptMsg);$data = $encryptMsg;}return $data;// return $arry;}public function getPost(){$timestamp = $_GET['timestamp'];$nonce = $_GET["nonce"];$msg_signature = $_GET['msg_signature'];$encrypt_type = (isset($_GET['encrypt_type']) && ($_GET['encrypt_type'] == 'aes')) ? "aes" : "raw";$arr = array("timestamp" => $timestamp,"nonce" => $nonce,"msg_signature" => $msg_signature,"encrypt_type" => $encrypt_type);return $arr;}private function write_log($log){print_r($log);}}?>三.信息匹配(search.php)<?phpclass chaxun{public function search($keyword){$list = $this->echolist($keyword);if (is_array($list) && ! empty($list)) {foreach ($list as $msg) {$record[] = array('title' => $msg['title'],'description' => $msg['description'],'picurl' => $msg['picurl'],'url' => $msg['url']);}} else {$record[] = array('title' => 'title','description' => '指令未知。