Msg.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. class Msg extends Controller{
  5. public function valid(){
  6. //获取随机字符串
  7. $echoStr = input("echostr");
  8. if($echoStr){
  9. // 验证接口的有效性,由于接口有效性的验证必定会传递echostr 参数
  10. if($this ->checkSignature()){
  11. echo $echoStr;
  12. exit;
  13. }
  14. }else{
  15. $this->responseMsg();
  16. }
  17. }
  18. protected function checkSignature()
  19. {
  20. // 微信加密签名
  21. $signature = input("signature");
  22. $timestamp = input("timestamp");//时间戳
  23. $nonce =input("nonce");//随机数
  24. $token = "weixin"; //token值,必须和你设置的一样
  25. $tmpArr =array($token,$timestamp,$nonce);
  26. sort($tmpArr,SORT_STRING);
  27. $tmpStr = implode($tmpArr);
  28. $tmpStr =sha1($tmpStr);
  29. if($tmpStr == $signature){
  30. return true;
  31. }else{
  32. return false;
  33. }
  34. }
  35. function responseMsg()
  36. {
  37. $postStr = file_get_contents('php://input','r');
  38. if(empty($postStr)){
  39. echo "";
  40. exit;
  41. }
  42. libxml_disable_entity_loader(true);
  43. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  44. // 发送方账号
  45. $fromUsername = $postObj->FromUserName;
  46. //开发者账号
  47. $toUsername = $postObj->ToUserName;
  48. // 文本信息内容,仅仅针对文本消息有值
  49. $keyword = trim($postObj->Content);
  50. $time = time();
  51. // 文字模板
  52. $textTpl = "<xml>
  53. <ToUserName><![CDATA[%s]]></ToUserName>
  54. <FromUserName><![CDATA[%s]]></FromUserName>
  55. <CreateTime>%s</CreateTime>
  56. <MsgType><![CDATA[%s]]></MsgType>
  57. <Content><![CDATA[%s]]></Content>
  58. <FuncFlag>0</FuncFlag>
  59. </xml>";
  60. // 图片模板
  61. $picTpl="<xml>
  62. <ToUserName><![CDATA[%s]]></ToUserName>
  63. <FromUserName><![CDATA[%s]]></FromUserName>
  64. <CreateTime>%s</CreateTime>
  65. <MsgType><![CDATA[%s]]></MsgType>
  66. <Image>
  67. <MediaId><![CDATA[%s]]></MediaId>
  68. </Image>
  69. </xml>";
  70. // 语音模板
  71. $voiceTpl="<xml>
  72. <ToUserName><![CDATA[%s]]></ToUserName>
  73. <FromUserName><![CDATA[%s]]></FromUserName>
  74. <CreateTime>%s</CreateTime>
  75. <MsgType><![CDATA[%s]]></MsgType>
  76. <Voice>
  77. <MediaId><![CDATA[%s]]></MediaId>
  78. </Voice>
  79. </xml>";
  80. // 视频模板
  81. $VideoTpl="<xml>
  82. <ToUserName><![CDATA[%s]]></ToUserName>
  83. <FromUserName><![CDATA[%s]]></FromUserName>
  84. <CreateTime>%s</CreateTime>
  85. <MsgType><![CDATA[%s]]></MsgType>
  86. <Video>
  87. <MediaId><![CDATA[%s]]></MediaId>
  88. <Title><![CDATA[%s]]></Title>
  89. <Description><![CDATA[%s]]></Description>
  90. </Video>
  91. </xml>";
  92. // 图文模板
  93. $newsTpc="<xml>
  94. <ToUserName><![CDATA[%s]]></ToUserName>
  95. <FromUserName><![CDATA[%s]]></FromUserName>
  96. <CreateTime>%s</CreateTime>
  97. <MsgType><![CDATA[%s]]></MsgType>
  98. <ArticleCount>%d</ArticleCount>
  99. <Articles>%s</Articles>
  100. </xml>";
  101. $msgType =$postObj->MsgType;
  102. if($msgType == "text")
  103. {
  104. // 根据keyword表中的字段进行相等匹配
  105. $info = db('Keyword')->where(array('keyword'=>$keyword))->find();
  106. if(!$info){
  107. //针对没有匹配的关键词使用机器人回复
  108. $url ="http://www.tuling123.com/openapi/api?key=96308475006241449b53013d66f8e387&info=".$keyword;
  109. $result = file_get_contents($url);
  110. $result = json_decode($result,true);
  111. if($result['code'] == 100000){
  112. // 回复文本消息
  113. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $result['text']);
  114. }elseif ($result['code'] == 200000) {
  115. $str = '<a href="'.$result['url'].'">'.$result['text'].'</a>';
  116. // 机器人中区分为链接
  117. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $str);
  118. }elseif ($result['code'] ==302000) {
  119. // 机器人中的新闻
  120. $data = $result['list'];
  121. for($i=0;$i<8;$i++){
  122. $Articles ="<item>
  123. <Title><![CDATA[{$data[$i]['article']}]]></Title>
  124. <Description><![CDATA[{$data[$i]['article']}]]></Description>
  125. <PicUrl><![CDATA[{$data[$i]['icon']}]]></PicUrl>
  126. <Url><![CDATA[{$data[$i]['detailurl']}]]></Url>
  127. </item>";
  128. }
  129. $count = 1;
  130. $resultStr = sprintf($newsTpc, $fromUsername, $toUsername, $time, 'news',$count,$Articles);
  131. }else{
  132. // 回复文本消息
  133. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', '抱歉没有理解,再说一遍问题');
  134. }
  135. echo $resultStr;
  136. // file_put_contents('2',33333);
  137. exit;
  138. }
  139. $content_id = $info['content_id'];
  140. $contents = db('Contents')->where("id=%d",$content_id)->find();
  141. if($contents['type']=='text'){
  142. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contents['content']);
  143. }elseif(($contents['type'] == 'image') ||
  144. ($contents['type'] == 'voice')){
  145. $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $contents['type'],$contents['mediaid']);
  146. }elseif($contents['type']=='video'){
  147. $resultStr = sprintf($VideoTpl, $fromUsername, $toUsername, $time, 'video',$contents['mediaid'],$contents['title'],$contents['description']);
  148. }elseif($contents['type']=='news'){
  149. // 从新闻表中查询数据
  150. $data = D('news')->where("id in (%s)",$contents['content'])->select();
  151. foreach ($data as $key => $value) {
  152. $Articles .="<item>
  153. <Title><![CDATA[{$value['title']}]]></Title>
  154. <Description><![CDATA[{$value['description']}]]></Description>
  155. <PicUrl><![CDATA[{$value['picurl']}]]></PicUrl>
  156. <Url><![CDATA[{$value['url']}]]></Url>
  157. </item>";
  158. }
  159. $count = count($data);
  160. $resultStr = sprintf($newsTpc, $fromUsername, $toUsername, $time, 'news',$count,$Articles);
  161. }else{
  162. // 音乐的回复内容
  163. $resultStr = '';
  164. }
  165. echo $resultStr;
  166. // if($keyword=="图片"){
  167. // // 关于此MediaId需要从素材库中获得,没有可以使用临时消息返回的媒体id
  168. // $MediaId="y-da216eEhArJ4yjtvSEW9EggxnAi8QWrfLqYosZej1uARvtx2HfUS4sO2XMOwwQ";
  169. // $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, 'image', $MediaId);
  170. // echo $resultStr;
  171. // }elseif($keyword=="语音"){
  172. // // 关于此MediaId需要从素材库中获得,没有可以使用临时消息返回的媒体id
  173. // $MediaId="BRxxaBbcDsoqCcISaWYJANG-ZL6DP0ezGFNadYhR9QdUlPqRODGS5apeFaTnpyOV";
  174. // $resultStr = sprintf($voiceTpl, $fromUsername, $toUsername, $time, 'voice', $MediaId);
  175. // echo $resultStr;
  176. // }elseif($keyword=="视频"){
  177. // //关于此MediaId需要从素材库中获得,没有可以使用临时消息返回的媒体id
  178. // $MediaId="xxMyAoPbUt1u3q5Z95xrhafNzyvL3Tg08E-9Ub2m6db_Elj4XAJHr2pUOqLhREyB";
  179. // $Title = $Description ="视频还是好看的";
  180. // $resultStr = sprintf($VideoTpl, $fromUsername, $toUsername, $time, 'video', $MediaId, $Title,$Description);
  181. // echo $resultStr;
  182. // }elseif($keyword=="图文"){
  183. // $data = array(
  184. // array('Title'=>'图文消息','Description'=>'效果好像还不错啊','PicUrl'=>'http://mmbiz.qpic.cn/mmbiz_jpg/E3TENE8JsTAqus3ic5qEtt4wl14ibBu4UaobarzTVOP18Awt83hkZM0aI9XStapN4xay6JI4lfm0H7QnKSfxQyVA/0','Url'=>'http://xiaomi.com')
  185. // );
  186. // file_put_contents('2',$data[0]['Title']);
  187. // for ($i=0; $i <count($data); $i++) {
  188. // $Articles .="<item>
  189. // <Title><![CDATA[{$data[$i]['Title']}]]></Title>
  190. // <Description><![CDATA[{$data[$i]['Description']}]]></Description>
  191. // <PicUrl><![CDATA[{$data[$i]['PicUrl']}]]></PicUrl>
  192. // <Url><![CDATA[{$data[$i]['Url']}]]></Url>
  193. // </item>";
  194. // }
  195. // $count = count($data);
  196. // $resultStr = sprintf($newsTpc, $fromUsername, $toUsername, $time, 'news',$count,$Articles);
  197. // echo $resultStr;
  198. // }else{
  199. // // 接受一个文本消息
  200. // $contentStr = "Welcome to wechat world!";
  201. // $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  202. // echo $resultStr;
  203. // }
  204. }elseif($msgType=="image"){
  205. $contentStr = "图片消息MediaId为:".$postObj->MediaId."图片地址为:".$postObj->PicUrl;
  206. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
  207. echo $resultStr;
  208. }elseif($msgType=="voice"){
  209. $contentStr ="语音消息MediaId为:".$postObj->MediaId.'具体内容为:'.$postObj->Recognition;
  210. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
  211. echo $resultStr;
  212. }elseif($msgType=="video"){
  213. $contentStr ="视频消息MediaId为:".$postObj->MediaId;
  214. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
  215. echo $resultStr;
  216. }elseif($msgType=="location"){
  217. $Locatiuon_X = $postObj->Location_X;
  218. $Locatiuon_Y = $postObj->Location_Y;
  219. $ak ="vEuxcgoZb0cWgLr7DoRXrwK8hIbYfCz9";
  220. $url ="http://api.map.baidu.com/geocoder/v2/?location=". $Locatiuon_X.','.$Locatiuon_Y."&output=json&pois=1&ak=".$ak;
  221. $json = file_get_contents($url);
  222. $result =json_decode($json,true);
  223. $contentStr ="位置为".$result['result']['formatted_address']."经度为".$Locatiuon_X.','.$Locatiuon_Y;
  224. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
  225. echo $resultStr;
  226. }elseif($msgType=="link"){
  227. $contentStr ="消息的标题为:".$postObj->Title;
  228. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
  229. echo $resultStr;
  230. }elseif($msgType=="event"){
  231. // 表示为事件
  232. $Event =$postObj->Event;//获取事件的类型
  233. if($Event=="CLICK"){
  234. // 表示为菜单的点击事件
  235. $EventKey =$postObj->EventKey;
  236. if($EventKey =="info"){
  237. $contentStr ="本人年芳十八岁!";
  238. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
  239. echo $resultStr;
  240. }else{
  241. $contentStr ="本人年芳二十岁!";
  242. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
  243. echo $resultStr;
  244. }
  245. }elseif($Event=="subscribe"){
  246. // 关注
  247. $contentStr ="欢迎关注保修系统";
  248. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,'text', $contentStr);
  249. echo $resultStr;
  250. }elseif($Event=="unsubscribe"){
  251. // 取消关注 不能回复消息
  252. }
  253. }else{
  254. echo "inptu somthing....";
  255. }
  256. }
  257. }