1. 实现微信接入
  2. 实现功能 当用户关注时 回复文本消息你好
  3. 当用户在公众号中回复内容时,回复图片消息。
  4. 当用户在公众号发送固定内容(只要内容中存在指定内容即可,未必全匹配)时,回复图文消息。
    扩展:当用户发送指定内容时,随机发送一种类型消息

演示图

共4个文件 index.php config.php Message.php WeChat.php

index.php 入口文件

  1. <?php
  2. //封装代码调用
  3. include 'config.php';
  4. include 'WeChat.php';
  5. include 'Message.php';
  6. $obj = new Message();
  7. $obj->postText();

config.php 定义TOKEY

  1. <?php
  2. define( 'TOKEN', 'yuanyiruciphp' );

WeChat.php 与微信接入文件

  1. <?php
  2. class WeChat
  3. {
  4. private $token = TOKEN;
  5. public function checkSignature()
  6. {
  7. $signature = $_GET['signature'];
  8. $timestamp = $_GET['timestamp'];
  9. $nonce = $_GET['nonce'];
  10. $echostr = $_GET['echostr'];
  11. $tmpArr = array( $this->token, $timestamp, $nonce );
  12. sort( $tmpArr, SORT_STRING );
  13. $tmpStr = implode( $tmpArr );
  14. $tmpStr = sha1( $tmpStr );
  15. //进行比对
  16. if ( $tmpStr == $signature ) {
  17. echo $echostr;
  18. } else {
  19. echo false;
  20. }
  21. }
  22. }

Message.php 消息会话管理文件

  1. <?php
  2. class Message extends WeChat
  3. {
  4. function postText()
  5. {
  6. if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) {
  7. //开发者服务器验证
  8. $this->checkSignature();
  9. } elseif ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  10. //接收
  11. $xml = file_get_contents( 'php://input' );
  12. if ( !empty( $xml ) ) {
  13. $obj = simplexml_load_string( $xml, 'SimpleXMLElement', LIBXML_NOCDATA );
  14. $toUser = $obj->FromUserName;
  15. $formUser = $obj->ToUserName;
  16. $time = time();
  17. //如果是关注
  18. if ( strtolower( $obj->MsgType ) == 'event' ) {
  19. if ( strtolower( $obj->Event ) == 'subscribe' ) {
  20. $msgType = 'text';
  21. $content = '你好,感谢你的关注';
  22. //回复用户关注的相关信息
  23. $this->text( $toUser, $formUser, $time, $msgType, $content );
  24. }
  25. }
  26. //如果发送文本消息
  27. if ( strtolower( $obj->MsgType ) == 'text' && preg_match( '/你好/S', $obj->Content ) ) {
  28. $title = '带有你好内容的回复';
  29. $description = '带有你好内容的回复内容介绍带有你好内容的回复内容介绍';
  30. $picUrl = 'https://www.php.cn/static/images/logo1.png';
  31. $url = 'https://www.php.cn/';
  32. $this->imgText( $toUser, $formUser, $time, 1, $title, $description, $picUrl, $url );
  33. } elseif ( strtolower( $obj->MsgType ) == 'text' && preg_match( '/开心/S', $obj->Content ) ) {
  34. $msgType = 'text';
  35. $content = '带有开心的文本回复';
  36. $this->text( $toUser, $formUser, $time, $msgType, $content );
  37. } else {
  38. $title = '任意消息回复';
  39. $description = '任意消息回复内容介绍任意消息回复内容介绍';
  40. $picUrl = 'https://img.php.cn/upload/course/000/000/001/60b5cd439a6b7169.png';
  41. $url = 'https://www.php.cn/';
  42. $this->imgText( $toUser, $formUser, $time, 1, $title, $description, $picUrl, $url );
  43. }
  44. }
  45. }
  46. }
  47. //回复文本消息
  48. protected function text( $toUser, $formUser, $time, $msgtype, $content )
  49. {
  50. $templade = "<xml>
  51. <ToUserName><![CDATA[%s]]></ToUserName>
  52. <FromUserName><![CDATA[%s]]></FromUserName>
  53. <CreateTime>%s</CreateTime>
  54. <MsgType><![CDATA[%s]]></MsgType>
  55. <Content><![CDATA[%s]]></Content>
  56. </xml>
  57. ";
  58. $info = sprintf( $templade, $toUser, $formUser, $time, $msgtype, $content );
  59. echo $info;
  60. }
  61. //回复图文消息
  62. protected function imgText( $toUser, $formUser, $time, $num, $title, $description, $picurl, $url )
  63. {
  64. $templade = "<xml>
  65. <ToUserName><![CDATA[%s]]></ToUserName>
  66. <FromUserName><![CDATA[%s]]></FromUserName>
  67. <CreateTime>%s</CreateTime>
  68. <MsgType><![CDATA[news]]></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. </Articles>
  78. </xml>";
  79. $info = sprintf( $templade, $toUser, $formUser, $time, $num, $title, $description, $picurl, $url );
  80. echo $info;
  81. }
  82. }

更多相关文章

  1. 使用easywechat完成微信接入,并且实现对接收不同消息的回复
  2. EasyWeChat的简单使用
  3. 【前端 · 面试 】HTTP 总结(二)—— HTTP 消息
  4. 系统控制—清除历史消息
  5. python抓取微信撤回消息
  6. 谈谈使用JS库解决小程序跨页传递消息和数据问题的方法
  7. 数据的异构实战(二)手写迷你版同步工程
  8. RocketMQ入门到入土(一)新手也能看懂的原理和实战!
  9. RocketMQ入门到入土(二)事务消息&顺序消息

随机推荐

  1. jQuery在.each循环中获取输入值
  2. Javascript字符串替换为动态创建的模式。
  3. Qunit:如何在不知道acceptCallCount的情况
  4. 如何从控制器内的javascript函数调用角范
  5. 使用单个函数隐藏多个输入
  6. 在laravel中的Http Post使用fetch api给
  7. 在JavaScript中进行文件处理,第四部分:对象
  8. 深入理解javascript函数进阶系列第二篇—
  9. 是否有一个简单的库将JSON对象渲染为树?
  10. HTML使用Canvas实现弹幕功能