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; $customrevent = $postObj->Event; $latitude = $postObj->Location_X; $longitude = $postObj->Location_Y; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content>%s</Content> <FuncFlag>0</FuncFlag> </xml>"; require_once ('BaeMemcache.class.php');//首先开启百度bae缓存,大小任意,10M即可 $mem=new BaeMemcache;//实例化 $struser=$mem->get($fromUsername."key");//获取缓存值 if (empty($struser)) { if($keyword=="2"){ $mem->set($fromUsername."key",$fromUsername."2",$flag=0,$expire=0);//设置缓存变量$$fromUsername."key" $contentStr="开始和小黄鸡对话#退出"; }//55 elseif($keyword=="1"){ $jokeurl="http://api.94qing.com/?type=joke&msg=";//笑话API $contentStr=file_get_contents($jokeurl); $contentStr = str_replace("\n","",$contentStr);} else{ $contentStr="输入1看笑话,2和小黄鸡聊天"; } }//54 else{ if ($keyword=="#") {$contentStr="已退出聊天,输入1看笑话,2和小黄鸡聊天"; $struser=$mem->delete($fromUsername."key");//删除缓存 } else{ if ($struser==$fromUsername."2"){//判断是否同一用户发送以及是否发送小黄鸡 $simurl="http://sandbox.api.simsimi.com/request.p?key=966ee8bb-dc33-48ec-9006-ae45e11a4151&lc=ch&text='{$keyword}'";//小黄鸡地址 $simstr=file_get_contents($simurl);//读入文件 $simjson=json_decode($simstr);//json解析 $contentStr = $simjson->response;//读取小黄鸡回复内容 }//76 }//75 }//70 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$contentStr); echo $resultStr; } }
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; $customrevent = $postObj->Event; $latitude = $postObj->Location_X; $longitude = $postObj->Location_Y; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; require_once ('BaeMemcache.class.php');//首先开启百度bae缓 $mem=new BaeMemcache;//实例化 $dbname = 'SnXAHxsmUPuRqZkaWnjF'; $host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP'); $port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT'); $user = getenv('HTTP_BAE_ENV_AK'); $pwd = getenv('HTTP_BAE_ENV_SK'); $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true); if(!$link) { die("Connect Server Failed: " . mysql_error($link)); } if(!mysql_select_db($dbname,$link)) { die("Select Database Failed: " . mysql_error($link)); } if ($keyword=="笑话") {//80 $sql = "SELECT * FROM `BD` WHERE `user` = '{$fromUsername}' "; $query=mysql_query($sql); $rs=mysql_fetch_array($query); $name= $rs['user']; if($name==$fromUsername) { $jokeurl="http://api.94qing.com/?type=joke&msg=";//笑话API $contentStr=file_get_contents($jokeurl); $contentStr = str_replace("\n","",$contentStr); }//读入文件 else {$contentStr="请先绑定qq,输入qq号进行绑定。"; $mem->set($fromUsername."key","qq",$flag=0,$expire=0);//设置缓存变 } }//51 else{ $struser=$mem->get($fromUsername."key");//获取缓存值 if($struser=="qq") { $sql = "SELECT * FROM `BD` WHERE `qq` = '{$keyword}' "; $query=mysql_query($sql); $rs=mysql_fetch_array($query); $qq= $rs['qq']; if($qq==$keyword) {$contentStr="绑定成功。"; $sql="UPDATE `BD` SET `user`='{$fromUsername}' WHERE `qq` = '{$keyword}'"; mysql_query($sql); $struser=$mem->delete($fromUsername."key");//删除缓存 } else {$contentStr="qq号错误,和管理员联系。";} }//84 else {$contentStr="未绑定qq,无法操作,输入qq号";} }//81 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr); echo $resultStr; } }