.......
<!-- require_once ('BaeMemcache.class.php');
$mem = new BaeMemcache();
$mem->set("key","value",$flag = 0,$expire = 0);//设置缓存
key键名 value值 $flag标记 $exopire过期时间 单位为秒 0表示永久有效
$mem->get("key");//读取缓存
$mem->delete("$key");//删除缓存 -->
public function responseMsg() {
//get Post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$MsgType = $postObj->MsgType;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content>%s</Content>
<FuncFlag>0</FuncFlag>
</xml>"
require_once ('BaeMemcache.class.php');//开启百度BAE缓存,10MB即可
$mem = new BaeMemcache;//实例化
if($keyword == "天气") {
$mem->set($fromUsername."key",$fromUsername."天气",$flag=0,$expire=60);
//设置缓存变量$$fromUsername."key",60s后过期
$contentStr="请输入地区,如北京进行天气情况查询";
}else{
$struser = $mem->get($fromUsername."key");//获取缓存值
if($struser==$fromUsername."天气"){
//判断是否同一用户发送,以及是否发送过天气
$weatherurl="http://api.map.baidu.com/telematics/v2/weather?location={$keyword}&ak=1a3cfjiefjrejfei";
$apistr=file_get_contents($weatherurl);
$apiobj=simplexml_load_string($apistr);
$placeobj=$apiobj->currentCity;//读取城市
$todayobj=$apiobj->results->result[0]->date;//读取星期
$weatherobj=$apiobj->results->result[0]->weather;//读取天气
$windobj=$apiobj->results->result[0]->wind;//读取风力
$temobj = $apiobj->results->result[0]->temperature;//读取温度
$contentStr = "{$placeobj}{$todayobj}天气{$weatherobj},风力{$windobj},温度{$temobj}";
if($contentStr=="天气,风力,温度"){
$contentStr = "你输入的地区有误";
}else{
$contentStr="请先输入天气";
}
$struser = $mem->get($fromUsername."key");//获取缓存值
if(empty($struser)){
if($keyword=="2"){
//设置缓存变量$$fromUsername."key"
$mem->set($fromUsername."key",$fromUsername."2",$flag=0,$expire=0);
$contentStr="开始和小黄鸡对话#退出";
}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和小黄鸡聊天";
}
}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-ae45e11a415&1c=ch&text='{$keyword}'";//小黄鸡地址
$simstr=file_get_contents($simurl);//读入文件
$simjson=json_decode($simstr);//json解析
$contentStr = $simjson->response;//读取小黄鸡回复内容
}
}
}
}
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$contentStr);
echo $resultStr;
}
}}
.......
session
最新推荐文章于 2022-11-08 08:00:00 发布