Blogger Information
Blog 20
fans 0
comment 0
visits 12335
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实现原生微信接入 当用户关注/发送消息时对应内容回应
缘亦
Original
725 people have browsed it
  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. }
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