标签:des c style class blog code
<?php /** * wechat php test * version 1 */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); //$wechatObj->valid(); 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"]; //$FILE = "感谢您关注【金融知识平台】\n微信号:f_jinrong\n请回复序号:\n1. 天气查询\n2. 翻译查询\n3.小奴讲笑话\n4.快递查询\n5.在线点歌\n更多功能,敬请期待..."; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch($RX_TYPE) { case "text": $resultStr = $this->handleText($postObj); break; case "event": $resultStr = $this->handleEvent($postObj); break; default: $resultStr = "Unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; } } 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; } } /* * 处理用户输入为“文本”的功能函数 */ public function handleText($postObj) { $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $msgType = "text"; $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty( $keyword )) { /* * 用户状态数据库操作 */ //判断用户状态 include ‘mysql.php‘; $sql = "SELECT flag_id FROM user_flags WHERE from_user = ‘$fromUsername‘ LIMIT 0,1"; $result = _select_data($sql); while (!!$rows = mysql_fetch_array($result)) { $user_flag = $rows[flag_id]; } //判断用户存在的状态和新输入的状态 if(trim($keyword) <> $user_flag && is_numeric($keyword)) { $user_flag = ‘‘; $sql = "DELETE FROM user_flags WHERE from_user = ‘$fromUsername‘"; _delete_data($sql); } /* * 判断用户状态,当用户状态为空时 */ if (empty($user_flag)) { switch ($keyword) { case 1: //查询天气 $sql = "insert into user_flags(from_user,flag_id) values(‘$fromUsername‘,‘1‘)"; $contentStr = "请输入要查询天气的城市:如北京、上海、苏州"; break; case 2: //翻译 $sql = "insert into user_flags(from_user,flag_id) values(‘$fromUsername‘,‘2‘)"; $contentStr = "请输入要翻译的内容:如:早上好、good morning、おはよう"; break; case 3: $sql = "insert into user_flags(from_user,flag_id) values(‘$fromUsername‘,‘3‘)"; $contentStr = "请输入\"笑话\"小奴给你讲笑话听听"; break; case 4: $sql = "insert into user_flags(from_user,flag_id) values(‘$fromUsername‘,‘4‘)"; $contentStr = ‘输入格式例如为【申通.快递单号】,例如:申通.768422403144‘; break; case 5: $sql = "insert into user_flags(from_user,flag_id) values(‘$fromUsername‘,‘5‘)"; $contentStr = ‘请输入您想听的歌曲名,如:【突然好想你】‘; break; default: //其他 $sql = ""; $contentStr = "感谢您关注【金融知识平台】\n微信号:f_jinrong\n请回复序号:\n1. 天气查询\n2. 翻译查询\n3.小奴讲笑话\n4.快递查询\n5.在线点歌\n更多功能,敬请期待..."; break; } //判断并执行上面的插入语句 if (!empty($sql)) { _insert_data($sql); } } /* * 判断用户状态,当用户状态不为空时 */ else{ switch($user_flag) { case 1: $contentStr = $this->_weather($keyword); //查询天气 break; case 2: $contentStr = $this->_baiduDic($keyword); //翻译 break; case 3: if($keyword==‘笑话‘) { $contentStr = $this->_sayJoke(); //讲笑话 } else{ $contentStr = "请输入【笑话】小奴才能给你讲笑话!"; } break; case 4: if($keyword==‘帮助‘) { $contentStr = "感谢您关注【金融知识平台】\n微信号:f_jinrong\n请回复序号:\n1. 天气查询\n2. 翻译查询\n3.小奴讲笑话\n4.快递查询\n5.在线点歌\n更多功能,敬请期待..."; }else { $contentStr = $this->_checkExpress($keyword); //快递查询 } break; case 5: return $resultStr = $this->_listenMusic($postObj); break; default: $contentStr = "请重新输入功能代码"; return $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); break; } } return $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } } /* * 处理“事件”的功能函数 */ public function handleEvent($postObj) { $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $time = time(); $msgType = "text"; $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; $contentStr = ""; switch ($postObj->Event) { case "subscribe": $contentStr = "感谢您关注【金融知识平台】\n微信号:f_jinrong\n请回复序号:\n1. 天气查询\n2. 翻译查询\n3.小奴讲笑话\n4.快递查询\n5.在线点歌\n更多功能,敬请期待..."; break; default : $contentStr = "Unknow Event: ".$postObj->Event; break; } return $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); } /* * 功能1:实现查询天气的接口功能,参数为城市中文名称,返回天气查询结果 */ private function _weather($city) { include("weather_cityId.php"); $c_name=$weather_cityId[$city]; if(!empty($c_name)){ $json=file_get_contents("http://m.weather.com.cn/data/".$c_name.".html"); $data = json_decode($json); if(empty($data->weatherinfo)){ return $contentStr = "抱歉,没有查到\"".$city."\"的天气信息!"; } else { return $contentStr = "【".$data->weatherinfo->city."天气预报】\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."时发布"."\n\n实时天气\n".$data->weatherinfo->weather1." ".$data->weatherinfo->temp1." ".$data->weatherinfo->wind1."\n\n温馨提示:".$data->weatherinfo->index_d."\n\n明天\n".$data->weatherinfo->weather2." ".$data->weatherinfo->temp2." ".$data->weatherinfo->wind2."\n\n后天\n".$data->weatherinfo->weather3." ".$data->weatherinfo->temp3." ".$data->weatherinfo->wind3; } } else { return $contentStr = "抱歉,没有查到\"".$city."\"的天气信息!"; } } /* * 功能2:实现翻译接口功能,参数传入需要翻译的字符串,返回值为翻译结果 */ private function _baiduDic($keyword) { $tranurlaip = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=9peNkh97N6B9GGj9zBke9tGQ&q={$keyword}&from=auto&to=auto"; $transtr = file_get_contents($tranurlaip); $transon = json_decode($transtr); $contentStr = $transon->trans_result[0]->dst; return $contentStr; } /* * 功能3:实现随机产生一个笑话故事的功能 */ private function _sayJoke() { $apiurl = "http://api.ajaxsns.com/api.php?key=free&appid=0&msg=笑话"; $tran = file_get_contents($apiurl); $data = json_decode($tran); if($data) { //$contentStr = $data->content; $temp = array(‘{br}‘=>"\n"); $contentStr = mb_substr(strtr($data->content,$temp),0,-18,"utf-8"); } else { $contentStr = "请输入【笑话】小奴才能给你讲笑话!"; } return $contentStr; } /* * 功能4:查找快递,$keyword输入格式例如为【申通.快递单号】,例如:申通.768422403144 */ private function _checkExpress($keyword) { include ‘Kd_id.php‘; $str = explode(‘.‘, $keyword); //按照要求截取字符串成一个数组,具有两个字符串 $order = $str[1]; $id = $Kd_id[$str[0]]; $apiurl = "http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9&order={$order}&id={$id}&show=xml"; //api接口地址 $trans = file_get_contents($apiurl); $data = simplexml_load_string($trans); $Kd_status = $arr_status[strval($data->Status)]; //strval函数将对象转化为字符串 foreach ($data->Data->Order as $a) { foreach ($a->Time as $b) { foreach ($a->Content as $c) { $m.= "{$b}{$c}"."\n"; } } } if($data) { $contentStr = "你的快递单号{$order}{$Kd_status}\n{$m}"; } else { $contentStr = "您的快递单号不存在,请重新输入格式例如为【申通.768422403144】的格式查询快递。"; } return $contentStr; } /* * 功能5:根据歌名点歌功能,输入歌名即可,传入参数为输入对象$postObj */ private function _listenMusic($postObj) { $ret= "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> <FuncFlag><![CDATA[1]]></FuncFlag> </Music> </xml>"; //$recognition=$postObj->Recognition; 语音识别功能 //$keywordc= urlencode($recognition); 语音识别功能 $keyword = trim($postObj->Content); $musicapi = "http://box.zhangmen.baidu.com/x?op=12&count=1&title={$keyword}\$\$"; $simstr=file_get_contents($musicapi); $musicobj=simplexml_load_string($simstr); $i=0; foreach($musicobj->url as $itemobj) { $encode = $itemobj->encode; $decode = $itemobj->decode; $removedecode = end(explode(‘&‘, $decode)); if($removedecode<>"") { $removedecode="&".$removedecode; } $decode = str_replace($removedecode,"", $decode); $musicurl= str_replace(end(explode(‘/‘, $encode)) ,$decode,$encode); break; } $resultStr = sprintf($ret, $postObj->FromUserName, $postObj->ToUserName,time(), $keyword,$musicurl,$musicurl); return $resultStr; } } ?>
请关注 金融知识平台 微信公众帐号,金融知识平台 基于SAE 平台开发,针对于主流的微信功能进行开发测试。
您可以关注 金融知识平台 公众帐号进行功能测试,以及获取新的应用开发。
1. 登录微信客户端,通讯录 -> 添加朋友 -> 查找公众号 -> f_jinrong,查找并关注。
2. 扫描二维码:
00微信公众平台 - 以上五大功能整合,完整的代码。,布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/yyfz/p/3775911.html