当前位置:Gxlcms > PHP教程 > 微信公众平台-发送被动响应消息-PHP示范

微信公众平台-发送被动响应消息-PHP示范

时间:2021-07-01 10:21:17 帮助过:72人阅读

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

?

更多信息查看:http://mp.weixin.qq.com/wiki/index.php?title=发送被动响应消息

人气教程排行