Blogger Information
Blog 43
fans 0
comment 0
visits 30449
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
微信公众号接入与发送接收消息
橙絮圆
Original
1605 people have browsed it

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

作业标题: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. }

    匹配数字回复图文消息

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post