php 自动回复消息


  
  
  1. <?php
  2. $testObj = new Test();
  3. if(! empty($_GET[ 'echostr'])){
  4. $testObj->valid();
  5. } else{
  6. $testObj->responseMsg();
  7. }
  8. exit;
  9. class Test
  10. {
  11. /**
  12. * 绑定url、token信息
  13. */
  14. public function valid(){
  15. $echoStr = $_GET[ "echostr"];
  16. if ( $this->checkSignature()) {
  17. echo $echoStr;
  18. }
  19. exit();
  20. }
  21. /**
  22. * 检查签名,确保请求是从微信发过来的
  23. */
  24. private function checkSignature()
  25. {
  26. $signature = $_GET[ "signature"];
  27. $timestamp = $_GET[ "timestamp"];
  28. $nonce = $_GET[ "nonce"];
  29. $token = "test123"; //与在微信配置的token一致,不可泄露
  30. $tmpArr = array($token, $timestamp, $nonce);
  31. sort($tmpArr);
  32. $tmpStr = implode( $tmpArr );
  33. $tmpStr = sha1( $tmpStr );
  34. if( $tmpStr == $signature ){
  35. return true;
  36. } else{
  37. return false;
  38. }
  39. }
  40. /**
  41. * 接收消息,并自动发送响应信息
  42. */
  43. public function responseMsg(){
  44. //验证签名
  45. if ( $this->checkSignature()){
  46. $postStr = $GLOBALS[ "HTTP_RAW_POST_DATA"];
  47. $this->log_request_info();
  48. //提取post数据
  49. if (! empty($postStr)){
  50. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  51. $fromUsername = $postObj->FromUserName; //发送人
  52. $toUsername = $postObj->ToUserName; //接收人
  53. $MsgType = $postObj->MsgType; //消息类型
  54. $MsgId = $postObj->MsgId; //消息id
  55. $time = time(); //当前时间做为回复时间
  56. //如果是文本消息(表情属于文本信息)
  57. if($MsgType == 'text'){
  58. $content = trim($postObj->Content); //消息内容
  59. if(! empty( $content )){
  60. //如果文本内容是图文,则回复图文信息,否则回复文本信息
  61. if($content == "图文"){
  62. //回复图文消息,ArticleCount图文消息个数,多条图文消息信息,默认第一个item为大图
  63. $ArticleCount = 2;
  64. $newsTpl = "<xml>
  65. <ToUserName><![CDATA[%s]]></ToUserName>
  66. <FromUserName><![CDATA[%s]]></FromUserName>
  67. <CreateTime>%s</CreateTime>
  68. <MsgType><![CDATA[%s]]></MsgType>
  69. <ArticleCount>%s</ArticleCount>
  70. <Articles>
  71. <item>
  72. <Title><![CDATA[%s]]></Title>
  73. <Description><![CDATA[%s]]></Description>
  74. <PicUrl><![CDATA[%s]]></PicUrl>
  75. <Url><![CDATA[%s]]></Url>
  76. </item>
  77. <item>
  78. <Title><![CDATA[%s]]></Title>
  79. <Description><![CDATA[%s]]></Description>
  80. <PicUrl><![CDATA[%s]]></PicUrl>
  81. <Url><![CDATA[%s]]></Url>
  82. </item>
  83. </Articles>
  84. </xml>";
  85. $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, 'news',
  86. $ArticleCount, '我是图文信息', '我是描述信息', 'http://www.test.com/DocCenterService/image?photo_id=236',
  87. 'http://www.test.com', '爱城市网正式开通上线', '描述2', 'http://jn.test.com/ac/skins/img/upload/img/20131116/48171384568991509.png',
  88. 'http://www.test.com');
  89. echo $resultStr;
  90. $this->log($resultStr);
  91. } else{
  92. //回复文本信息
  93. $textTpl = "<xml>
  94. <ToUserName><![CDATA[%s]]></ToUserName>
  95. <FromUserName><![CDATA[%s]]></FromUserName>
  96. <CreateTime>%s</CreateTime>
  97. <MsgType><![CDATA[%s]]></MsgType>
  98. <Content><![CDATA[%s]]></Content>
  99. <FuncFlag>0</FuncFlag>
  100. </xml>";
  101. $contentStr = '你发送的信息是:接收人:'.$toUsername. ',发送人:'.$fromUsername. ',消息类型:'.$MsgType. ',消息内容:'.$content. ' www.icity365.com';
  102. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
  103. echo $resultStr;
  104. $this->log($resultStr);
  105. }
  106. } else{
  107. echo "Input something...";
  108. $this->log($resultStr);
  109. }
  110. //如果是图片消息
  111. } elseif ($MsgType == 'image'){
  112. $MediaId = $postObj->MediaId; //图片消息媒体id,可以调用多媒体文件下载接口拉取数据。
  113. $imageTpl = "<xml>
  114. <ToUserName><![CDATA[%s]]></ToUserName>
  115. <FromUserName><![CDATA[%s]]></FromUserName>
  116. <CreateTime>%s</CreateTime>
  117. <MsgType><![CDATA[%s]]></MsgType>
  118. <Image>
  119. <MediaId><![CDATA[%s]]></MediaId>
  120. </Image>
  121. </xml>";
  122. $resultStr = sprintf($imageTpl, $fromUsername, $toUsername, $time, $MsgType, $MediaId);
  123. echo $resultStr;
  124. $this->log( "自动响应图片信息");
  125. $this->log($resultStr);
  126. //如果是视频
  127. } else if($MsgType == 'video'){
  128. $MediaId = $postObj->MediaId; //视频消息媒体id,可以调用多媒体文件下载接口拉取数据。
  129. $ThumbMediaId = $postObj->ThumbMediaId; //视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
  130. $videoTpl = "<xml>
  131. <ToUserName><![CDATA[%s]]></ToUserName>
  132. <FromUserName><![CDATA[%s]]></FromUserName>
  133. <CreateTime>%s</CreateTime>
  134. <MsgType><![CDATA[%s]]></MsgType>
  135. <Video>
  136. <MediaId><![CDATA[%s]]></MediaId>
  137. <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
  138. <Title><![CDATA[%s]]></Title>
  139. <Description><![CDATA[%s]]></Description>
  140. </Video>
  141. </xml>";
  142. $resultStr = sprintf($videoTpl, $fromUsername, $toUsername, $time, $MsgType, $MediaId,$ThumbMediaId, '我是标题', '我是描述');
  143. echo $resultStr;
  144. $this->log( "自动响应视频信息".$ThumbMediaId);
  145. $this->log($resultStr);
  146. //如果是地理位置
  147. } else if($MsgType == 'location'){
  148. $Location_X = $postObj->Location_X; //维度
  149. $Location_Y = $postObj->Location_Y; //经度
  150. $Scale = $postObj->Scale; //地图缩放大小
  151. $Label = $postObj->Label; //地里位置信息
  152. //回复文本信息
  153. $textTpl = "<xml>
  154. <ToUserName><![CDATA[%s]]></ToUserName>
  155. <FromUserName><![CDATA[%s]]></FromUserName>
  156. <CreateTime>%s</CreateTime>
  157. <MsgType><![CDATA[%s]]></MsgType>
  158. <Content><![CDATA[%s]]></Content>
  159. <FuncFlag>0</FuncFlag>
  160. </xml>";
  161. $msgType = "text";
  162. $contentStr = '经度:'.$Location_Y. ',维度:'.$Location_X. ',地图缩放大小'.$Scale. ',地理位置信息:'.$Label;
  163. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  164. echo $resultStr;
  165. $this->log($resultStr);
  166. //如果是事件
  167. } else if($MsgType == 'event'){
  168. $Event = $postObj->Event;
  169. //subscribe(关注,也叫订阅)
  170. if($Event == 'subscribe'){
  171. $EventKey = $postObj->EventKey; //事件KEY值,qrscene_为前缀,后面为二维码的参数值
  172. //未关注时,扫描二维码
  173. if(! empty($EventKey)){
  174. $Ticket = $postObj->Ticket; //二维码的ticket,可用来换取二维码图片
  175. $this->log($fromUsername. '扫描二维码关注!EventKey='.$EventKey. ',Ticket='.$Ticket);
  176. } else{
  177. $this->log($fromUsername. '关注我了!');
  178. }
  179. //unsubscribe(取消关注)
  180. } elseif ($Event == 'unsubscribe'){
  181. $this->log($fromUsername. '取消关注我了!');
  182. //已关注时,扫描二维码事件
  183. } elseif($Event == 'SCAN' || $Event == 'scan'){
  184. $EventKey = $postObj->EventKey; //事件KEY值,是一个32位无符号整数,即创建二维码时的二维码scene_id
  185. $Ticket = $postObj->Ticket; //二维码的ticket,可用来换取二维码图片
  186. $this->log($fromUsername. '关注我了!EventKey='.$EventKey. ',Ticket='.$Ticket);
  187. //菜单点击事件
  188. } elseif($Event == 'CLICK'){
  189. $EventKey = $postObj->EventKey; //事件KEY值,与自定义菜单接口中KEY值对应
  190. //回复文本信息
  191. $textTpl = "<xml>
  192. <ToUserName><![CDATA[%s]]></ToUserName>
  193. <FromUserName><![CDATA[%s]]></FromUserName>
  194. <CreateTime>%s</CreateTime>
  195. <MsgType><![CDATA[%s]]></MsgType>
  196. <Content><![CDATA[%s]]></Content>
  197. <FuncFlag>0</FuncFlag>
  198. </xml>";
  199. $contentStr = '你点击了菜单,菜单项key='.$EventKey;
  200. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
  201. echo $resultStr;
  202. $this->log($resultStr);
  203. //其他事件类型
  204. } else{
  205. $this->log( '事件类型:'.$Event);
  206. }
  207. //其他消息类型,链接、语音等
  208. } else{
  209. //回复文本信息
  210. $textTpl = "<xml>
  211. <ToUserName><![CDATA[%s]]></ToUserName>
  212. <FromUserName><![CDATA[%s]]></FromUserName>
  213. <CreateTime>%s</CreateTime>
  214. <MsgType><![CDATA[%s]]></MsgType>
  215. <Content><![CDATA[%s]]></Content>
  216. <FuncFlag>0</FuncFlag>
  217. </xml>";
  218. $contentStr = '消息类型:'.$MsgType. '我们还没做处理。。。。【爱城市网】';
  219. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
  220. echo $resultStr;
  221. $this->log($resultStr);
  222. }
  223. } else {
  224. echo "";
  225. exit;
  226. }
  227. } else{
  228. $this->log( "验证签名未通过!");
  229. }
  230. }
  231. /**
  232. * 记录请求信息
  233. */
  234. function log_request_info() {
  235. $post = '';
  236. foreach($_POST as $key => $value) {
  237. $post = $post.$key. ' : '.$value. ' , ';
  238. }
  239. $get = '';
  240. foreach($_GET as $key => $value) {
  241. $get = $get.$key. ' : '.$value. ' , ';
  242. }
  243. $this->log( "get信息:".$get);
  244. $this->log( "post信息:".$post);
  245. }
  246. /**
  247. * 记录日志
  248. * @param $str
  249. * @param $mode
  250. */
  251. function log($str){
  252. $mode= 'a'; //追加方式写
  253. $file = "log.txt";
  254. $oldmask = @umask( 0);
  255. $fp = @fopen($file,$mode);
  256. @flock($fp, 3);
  257. if(!$fp)
  258. {
  259. Return false;
  260. }
  261. else
  262. {
  263. @fwrite($fp,$str);
  264. @fclose($fp);
  265. @umask($oldmask);
  266. Return true;
  267. }
  268. }
  269. }
  270. ?>

                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/tianzeyong">
                <img src="https://profile.csdnimg.cn/3/0/8/3_tianzeyong" class="avatar_pic" username="tianzeyong">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/13.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/tianzeyong" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">tianzeyong</a></span>
                                        </div>
                <div class="text"><span>发布了3 篇原创文章</span> · <span>获赞 3</span> · <span>访问量 9729</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=tianzeyong" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
发布了4 篇原创文章 · 获赞 1 · 访问量 227

猜你喜欢

转载自blog.csdn.net/weixin_43796856/article/details/104310355
今日推荐