valid();$wechatObj->responseMsg();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"]; //extract post data if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; //$type=$postobj->MsgType; $type =$postObj->MsgType; $customevent=$postObj->Event; $latitude=$postObj->Location_X; $longitude=$postObj->Location_Y; $keyword = trim($postObj->Content); $time = time(); $textTpl = ""; $musicTpl=" %s 0 "; if($type=="event" )//and $customrevent=="subscribe") { $msgType = "text"; $contentStr="感谢您的关注\n 回复1可查看联系方式 \n 回复2可还钱 \n 回复3可查看各种小吃"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } else { switch($type) { case "text": { $msgType = "text"; if(!empty( $keyword )) { if($keyword=="1") { $contentStr="广东工业大学工二馆,联系方式:18790652159,联系人:张星星"; } else if($keyword=="2") { $contentStr="请将钱打入工行卡88888888,于五个工作日内,谢谢配合"; } else if($keyword=="3") { $contentStr="饿了吗?河南小吃,广东小吃,台湾小吃,各种美味等着你哦"; } else $contentStr = "hi,感谢您的关注!您的请求不正确,请重新申请。祝您每天生活愉快,牙齿天天晒------熳熳"; } else { $contentStr= "Input something..."; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); if($keyword=="音乐") { $Title="纯真年代"; $Description="离婚前规则主题曲"; $MusicUrl="http://1.zhangxuman.sinaapp.com/music/1.mp3"; $HQMusicUrl= $MusicUrl; $msgType="music"; $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$Title,$Description,$MusicUrl,$HQMusicUrl); } break; } case "voice": { $msgType = "text"; $contentStr="语音功能尚未完善,请输入文本信息"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } case "image": { $msgType = "text"; $contentStr="你的图片很漂亮"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } case "location": { $msgType = "text"; $contentStr="你的纬度是{$latitude},经度是{$longitude},我们已经锁定!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } case "link": { $msgType = "text"; $contentStr="你的链接有病毒吧!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } default : { $msgType = "text"; $contentStr="此功能尚未开发"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } } } echo $resultStr; } else { exit; } } function replymusic() //回复音乐 { $musicTpl=" %s <![CDATA[%s]]> "; $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } private function checkSignature() { $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 ) { return true; }else { return false; } }}?> 12345678 <![CDATA[纯真年代]]>
比上一篇博文多了一个“音乐”的功能,当输入“音乐”,就会回复“纯真年代”