微信公众号接入与发送接收消息

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


  1. 实现微信接入
    配置成功
  2. 实现功能 当用户关注时 回复文本消息你好
    回复
    代码

  1. <?php
  2. class Message extends WeChat
  3. {
  4. function postText()
  5. {
  6. //判断微信服务请求的方法 get 还是 post
  7. if($_SERVER['REQUEST_METHOD'] == 'GET'){
  8. // //接入验证
  9. $this->checkSignature();
  10. }elseif($_SERVER['REQUEST_METHOD'] == 'POST'){
  11. //读取数据
  12. $xml = file_get_contents('php://input');
  13. if(!empty($xml)){
  14. //将xml数据转换成对象
  15. $postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
  16. $toUser = $postObj->FromUserName;
  17. $formUser = $postObj->ToUserName;
  18. $time = time();
  19. //判断用户传入的类型 (文本 图文)
  20. if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){
  21. $title = '真正成功了才是成功';
  22. $description = '进入php.cn主页';
  23. $picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';
  24. $url = 'https://www.php.cn';
  25. $this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);
  26. }elseif($postObj->MsgType == 'event'){
  27. if($postObj->Event == 'subscribe'){
  28. //制作对应的回复内容
  29. $content = '你好';
  30. $this->text($toUser,$formUser,$time,$content);
  31. }
  32. }
  33. }
  34. }
  35. }
  36. /*
  37. * 制作文本消息
  38. */
  39. private function text($toUser,$formUser,$time,$content)
  40. {
  41. //制作对应的回复内容
  42. $templade = "<xml>
  43. <ToUserName><![CDATA[%s]]></ToUserName>
  44. <FromUserName><![CDATA[%s]]></FromUserName>
  45. <CreateTime>%s</CreateTime>
  46. <MsgType><![CDATA[text]]></MsgType>
  47. <Content><![CDATA[%s]]></Content>
  48. </xml>";
  49. echo sprintf($templade,$toUser,$formUser,$time,$content);
  50. }
  51. /*
  52. * 制作图文
  53. */
  54. private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url)
  55. {
  56. $str="<xml>
  57. <ToUserName><![CDATA[%s]]></ToUserName>
  58. <FromUserName><![CDATA[%s]]></FromUserName>
  59. <CreateTime>%s</CreateTime>
  60. <MsgType><![CDATA[news]]></MsgType>
  61. <ArticleCount>1</ArticleCount>
  62. <Articles>
  63. <item>
  64. <Title><![CDATA[%s]]></Title>
  65. <Description><![CDATA[%s]]></Description>
  66. <PicUrl><![CDATA[%s]]></PicUrl>
  67. <Url><![CDATA[%s]]></Url>
  68. </item>
  69. </Articles>
  70. </xml>
  71. ";
  72. echo sprintf($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);
  73. }
  74. }

3.当用户在公众号中回复内容时,回复图片消息。

代码


  1. <?php
  2. class Message extends WeChat
  3. {
  4. function postText()
  5. {
  6. //判断微信服务请求的方法 get 还是 post
  7. if($_SERVER['REQUEST_METHOD'] == 'GET'){
  8. // //接入验证
  9. $this->checkSignature();
  10. }elseif($_SERVER['REQUEST_METHOD'] == 'POST'){
  11. //读取数据
  12. $xml = file_get_contents('php://input');
  13. if(!empty($xml)){
  14. //将xml数据转换成对象
  15. $postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
  16. $toUser = $postObj->FromUserName;
  17. $formUser = $postObj->ToUserName;
  18. $time = time();
  19. //判断用户传入的类型 (文本 图文)
  20. if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){
  21. $title = '真正成功了才是成功';
  22. $description = '进入php.cn主页';
  23. $picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';
  24. $url = 'https://www.php.cn';
  25. $this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);
  26. }elseif($postObj->MsgType == 'event'){
  27. if($postObj->Event == 'subscribe'){
  28. //制作对应的回复内容
  29. $content = '你好';
  30. $this->text($toUser,$formUser,$time,$content);
  31. }
  32. }
  33. }
  34. }
  35. }
  36. /*
  37. * 制作文本消息
  38. */
  39. private function text($toUser,$formUser,$time,$content)
  40. {
  41. //制作对应的回复内容
  42. $templade = "<xml>
  43. <ToUserName><![CDATA[%s]]></ToUserName>
  44. <FromUserName><![CDATA[%s]]></FromUserName>
  45. <CreateTime>%s</CreateTime>
  46. <MsgType><![CDATA[text]]></MsgType>
  47. <Content><![CDATA[%s]]></Content>
  48. </xml>";
  49. echo sprintf($templade,$toUser,$formUser,$time,$content);
  50. }
  51. /*
  52. * 制作图文
  53. */
  54. private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url)
  55. {
  56. $str="<xml>
  57. <ToUserName><![CDATA[%s]]></ToUserName>
  58. <FromUserName><![CDATA[%s]]></FromUserName>
  59. <CreateTime>%s</CreateTime>
  60. <MsgType><![CDATA[news]]></MsgType>
  61. <ArticleCount>1</ArticleCount>
  62. <Articles>
  63. <item>
  64. <Title><![CDATA[%s]]></Title>
  65. <Description><![CDATA[%s]]></Description>
  66. <PicUrl><![CDATA[%s]]></PicUrl>
  67. <Url><![CDATA[%s]]></Url>
  68. </item>
  69. </Articles>
  70. </xml>
  71. ";
  72. echo sprintf($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);
  73. }
  74. }
  1. 当用户在公众号发送固定内容(只要内容中存在指定内容即可,未必全匹配)时,回复图文消息。 扩展:当用户发送指定内容时,随机发送一种类型消息

    1. <?php
    2. class Message extends WeChat
    3. {
    4. function postText()
    5. {
    6. //判断微信服务请求的方法 get 还是 post
    7. if($_SERVER['REQUEST_METHOD'] == 'GET'){
    8. // //接入验证
    9. $this->checkSignature();
    10. }elseif($_SERVER['REQUEST_METHOD'] == 'POST'){
    11. //读取数据
    12. $xml = file_get_contents('php://input');
    13. if(!empty($xml)){
    14. //将xml数据转换成对象
    15. $postObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
    16. $toUser = $postObj->FromUserName;
    17. $formUser = $postObj->ToUserName;
    18. $time = time();
    19. //判断用户传入的类型 (文本 图文)
    20. if($postObj->MsgType == 'text' && preg_match('/\d/S',$postObj->Content)){
    21. $title = '真正成功了才是成功';
    22. $description = '进入php.cn主页';
    23. $picurl = 'https://www.php.cn/static/images/index_php_new4.jpg?1';
    24. $url = 'https://www.php.cn';
    25. $this->imgText($toUser,$formUser,$time,$title,$description,$picurl,$url);
    26. }elseif($postObj->MsgType == 'event'){
    27. if($postObj->Event == 'subscribe'){
    28. //制作对应的回复内容
    29. $content = '你好';
    30. $this->text($toUser,$formUser,$time,$content);
    31. }
    32. }
    33. }
    34. }
    35. }
    36. /*
    37. * 制作文本消息
    38. */
    39. private function text($toUser,$formUser,$time,$content)
    40. {
    41. //制作对应的回复内容
    42. $templade = "<xml>
    43. <ToUserName><![CDATA[%s]]></ToUserName>
    44. <FromUserName><![CDATA[%s]]></FromUserName>
    45. <CreateTime>%s</CreateTime>
    46. <MsgType><![CDATA[text]]></MsgType>
    47. <Content><![CDATA[%s]]></Content>
    48. </xml>";
    49. echo sprintf($templade,$toUser,$formUser,$time,$content);
    50. }
    51. /*
    52. * 制作图文
    53. */
    54. private function imgText($toUser,$fromUser,$time,$title,$description,$picurl,$url)
    55. {
    56. $str="<xml>
    57. <ToUserName><![CDATA[%s]]></ToUserName>
    58. <FromUserName><![CDATA[%s]]></FromUserName>
    59. <CreateTime>%s</CreateTime>
    60. <MsgType><![CDATA[news]]></MsgType>
    61. <ArticleCount>1</ArticleCount>
    62. <Articles>
    63. <item>
    64. <Title><![CDATA[%s]]></Title>
    65. <Description><![CDATA[%s]]></Description>
    66. <PicUrl><![CDATA[%s]]></PicUrl>
    67. <Url><![CDATA[%s]]></Url>
    68. </item>
    69. </Articles>
    70. </xml>
    71. ";
    72. echo sprintf($str,$toUser,$fromUser,$time,$title,$description,$picurl,$url);
    73. }
    74. }

    匹配数字回复图文消息

更多相关文章

  1. easywechat实现微信接入并不同消息回复+根据项目提供数据字典,对
  2. 实战原生微信接入和消息回复
  3. 实现原生微信接入 当用户关注/发送消息时对应内容回应
  4. 使用easywechat完成微信接入,并且实现对接收不同消息的回复
  5. 作业内容:oop基础:请举例实例演绎以下难点 1. 类(对象抽象化的结
  6. CSS_0702作业
  7. VSCODE的安装配置与插件安装及HTTP协议初识
  8. HTML入门笔记1
  9. javascript内容整理

随机推荐

  1. android 创建动态View
  2. Android 实现文件(图片)上传
  3. android bugly使用
  4. android 使用DataBinding问题总结
  5. android打电话和发短信
  6. 2013.6.18 Android SDK和最新ADT下载地址
  7. Android 之开机启动Service
  8. android判断软件是否第一次运行的方法
  9. Linux下安装配置Android开发环境
  10. Error:Execution failed for task ':app: