checkSignature()){
echo $echoStr;
exit;
}
}else{
$this->responseMsg();
}
}
protected function checkSignature()
{
// 微信加密签名
$signature = input("signature");
$timestamp = input("timestamp");//时间戳
$nonce =input("nonce");//随机数
$token = "weixin"; //token值,必须和你设置的一样
$tmpArr =array($token,$timestamp,$nonce);
sort($tmpArr,SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr =sha1($tmpStr);
if($tmpStr == $signature){
return true;
}else{
return false;
}
}
function responseMsg()
{
$postStr = file_get_contents('php://input','r');
if(empty($postStr)){
echo "";
exit;
}
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
// 发送方账号
$fromUsername = $postObj->FromUserName;
//开发者账号
$toUsername = $postObj->ToUserName;
// 文本信息内容,仅仅针对文本消息有值
$keyword = trim($postObj->Content);
$time = time();
// 文字模板
$textTpl = "
%s
0
";
// 图片模板
$picTpl="
%s
";
// 语音模板
$voiceTpl="
%s
";
// 视频模板
$VideoTpl="
%s
";
// 图文模板
$newsTpc="
%s
%d
%s
";
$msgType =$postObj->MsgType;
if($msgType == "text")
{
// 根据keyword表中的字段进行相等匹配
$info = db('Keyword')->where(array('keyword'=>$keyword))->find();
if(!$info){
//针对没有匹配的关键词使用机器人回复
$url ="http://www.tuling123.com/openapi/api?key=96308475006241449b53013d66f8e387&info=".$keyword;
$result = file_get_contents($url);
$result = json_decode($result,true);
if($result['code'] == 100000){
// 回复文本消息
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $result['text']);
}elseif ($result['code'] == 200000) {
$str = ''.$result['text'].'';
// 机器人中区分为链接
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $str);
}elseif ($result['code'] ==302000) {
// 机器人中的新闻
$data = $result['list'];
for($i=0;$i<8;$i++){
$Articles ="-
";
}
$count = 1;
$resultStr = sprintf($newsTpc, $fromUsername, $toUsername, $time, 'news',$count,$Articles);
}else{
// 回复文本消息
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', '抱歉没有理解,再说一遍问题');
}
echo $resultStr;
// file_put_contents('2',33333);
exit;
}
$content_id = $info['content_id'];
$contents = db('Contents')->where("id=%d",$content_id)->find();
if($contents['type']=='text'){
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contents['content']);
}elseif(($contents['type'] == 'image') ||
($contents['type'] == 'voice')){
$resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $contents['type'],$contents['mediaid']);
}elseif($contents['type']=='video'){
$resultStr = sprintf($VideoTpl, $fromUsername, $toUsername, $time, 'video',$contents['mediaid'],$contents['title'],$contents['description']);
}elseif($contents['type']=='news'){
// 从新闻表中查询数据
$data = D('news')->where("id in (%s)",$contents['content'])->select();
foreach ($data as $key => $value) {
$Articles .="-
";
}
$count = count($data);
$resultStr = sprintf($newsTpc, $fromUsername, $toUsername, $time, 'news',$count,$Articles);
}else{
// 音乐的回复内容
$resultStr = '';
}
echo $resultStr;
// if($keyword=="图片"){
// // 关于此MediaId需要从素材库中获得,没有可以使用临时消息返回的媒体id
// $MediaId="y-da216eEhArJ4yjtvSEW9EggxnAi8QWrfLqYosZej1uARvtx2HfUS4sO2XMOwwQ";
// $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, 'image', $MediaId);
// echo $resultStr;
// }elseif($keyword=="语音"){
// // 关于此MediaId需要从素材库中获得,没有可以使用临时消息返回的媒体id
// $MediaId="BRxxaBbcDsoqCcISaWYJANG-ZL6DP0ezGFNadYhR9QdUlPqRODGS5apeFaTnpyOV";
// $resultStr = sprintf($voiceTpl, $fromUsername, $toUsername, $time, 'voice', $MediaId);
// echo $resultStr;
// }elseif($keyword=="视频"){
// //关于此MediaId需要从素材库中获得,没有可以使用临时消息返回的媒体id
// $MediaId="xxMyAoPbUt1u3q5Z95xrhafNzyvL3Tg08E-9Ub2m6db_Elj4XAJHr2pUOqLhREyB";
// $Title = $Description ="视频还是好看的";
// $resultStr = sprintf($VideoTpl, $fromUsername, $toUsername, $time, 'video', $MediaId, $Title,$Description);
// echo $resultStr;
// }elseif($keyword=="图文"){
// $data = array(
// array('Title'=>'图文消息','Description'=>'效果好像还不错啊','PicUrl'=>'http://mmbiz.qpic.cn/mmbiz_jpg/E3TENE8JsTAqus3ic5qEtt4wl14ibBu4UaobarzTVOP18Awt83hkZM0aI9XStapN4xay6JI4lfm0H7QnKSfxQyVA/0','Url'=>'http://xiaomi.com')
// );
// file_put_contents('2',$data[0]['Title']);
// for ($i=0; $i MediaId."图片地址为:".$postObj->PicUrl;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
echo $resultStr;
}elseif($msgType=="voice"){
$contentStr ="语音消息MediaId为:".$postObj->MediaId.'具体内容为:'.$postObj->Recognition;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
echo $resultStr;
}elseif($msgType=="video"){
$contentStr ="视频消息MediaId为:".$postObj->MediaId;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
echo $resultStr;
}elseif($msgType=="location"){
$Locatiuon_X = $postObj->Location_X;
$Locatiuon_Y = $postObj->Location_Y;
$ak ="vEuxcgoZb0cWgLr7DoRXrwK8hIbYfCz9";
$url ="http://api.map.baidu.com/geocoder/v2/?location=". $Locatiuon_X.','.$Locatiuon_Y."&output=json&pois=1&ak=".$ak;
$json = file_get_contents($url);
$result =json_decode($json,true);
$contentStr ="位置为".$result['result']['formatted_address']."经度为".$Locatiuon_X.','.$Locatiuon_Y;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
echo $resultStr;
}elseif($msgType=="link"){
$contentStr ="消息的标题为:".$postObj->Title;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
echo $resultStr;
}elseif($msgType=="event"){
// 表示为事件
$Event =$postObj->Event;//获取事件的类型
if($Event=="CLICK"){
// 表示为菜单的点击事件
$EventKey =$postObj->EventKey;
if($EventKey =="info"){
$contentStr ="本人年芳十八岁!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
echo $resultStr;
}else{
$contentStr ="本人年芳二十岁!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
echo $resultStr;
}
}elseif($Event=="subscribe"){
// 关注
$contentStr ="欢迎关注保修系统";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
echo $resultStr;
}elseif($Event=="unsubscribe"){
// 取消关注 不能回复消息
}
}else{
echo "inptu somthing....";
}
}
}