Blogger Information
Blog 20
fans 0
comment 0
visits 10863
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实战原生微信接入和消息回复
麦兜的故事
Original
573 people have browsed it

1. 实现微信接入

  1. <?php
  2. class WechatAccess
  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. // $token = TOKEN;
  12. $tmpArr = array($this->token, $timestamp, $nonce);
  13. sort($tmpArr, SORT_STRING);
  14. $tmpStr = implode($tmpArr);
  15. $tmpStr = sha1($tmpStr);
  16. if ($tmpStr == $signature) {
  17. return $echostr;
  18. } else {
  19. return false;
  20. }
  21. }
  22. }

2. 实现功能 当用户关注时 回复文本消息你好

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

4. 当用户在公众号发送固定内容(只要内容中存在指定内容即可,未必全匹配)时,回复图文消息。

扩展:当用户发送指定内容时,随机发送一种类型消息

  1. class Message extends WechatAccess
  2. {
  3. function postText()
  4. {
  5. if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  6. $this->checkSignature();
  7. } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
  8. $xml = file_get_contents('php://input');
  9. if (!empty($xml)) {
  10. $postObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  11. $toUser = $postObj->FromUserName;
  12. $fromUser = $postObj->ToUserName;
  13. $time = time();
  14. if ($postObj->MsgType == 'event') {
  15. if ($postObj->Event == 'subscribe') {
  16. $content = '你这么好看,关注我就对了';
  17. $this->text($toUser, $fromUser, $time, $content);
  18. }
  19. } elseif ($postObj->MsgType == 'text' && preg_match('/\d/S', $postObj->Content)) {
  20. $content = '您好,有什么需要帮助你的吗?';
  21. $this->text($toUser, $fromUser, $time, $content);
  22. } elseif ($postObj->MsgType == 'text' && preg_match('/^文章/', $postObj->Content)) {
  23. $title = '叮!请查收『Dr.Yu开学护肤指南』· 文末正装礼';
  24. $description = '新学期,玩点厉害的,宠粉开始...';
  25. $picurl = 'https://img.zcool.cn/community/0114705c45d80ea801213f26f0c42c.gif';
  26. $url = 'https://php.cn';
  27. $this->ImgText($toUser, $fromUser, $time, $title, $description, $picurl, $url);
  28. } elseif ($postObj->MsgType == 'text' && preg_match('/^aa/', $postObj->Content)) {
  29. $title = '叮!请查收『Dr.Yu开学护肤指南』· 文末正装礼';
  30. $description = '新学期,玩点厉害的,宠粉开始...';
  31. $picurl = 'https://img.zcool.cn/community/0114705c45d80ea801213f26f0c42c.gif';
  32. $url = 'https://php.cn';
  33. $content = '随机';
  34. $arr = array($this->ImgText($toUser, $fromUser, $time, $title, $description, $picurl, $url), $this->text($toUser, $fromUser, $time, $content));
  35. array_rand($arr);
  36. } elseif ($postObj->MsgType == 'image') {
  37. }
  38. }
  39. }
  40. }
  41. private function text($toUser, $fromUser, $time, $content)
  42. {
  43. $str = "<xml>
  44. <ToUserName><![CDATA[%s]]></ToUserName>
  45. <FromUserName><![CDATA[%s]]></FromUserName>
  46. <CreateTime>%s</CreateTime>
  47. <MsgType><![CDATA[text]]></MsgType>
  48. <Content><![CDATA[%s]]></Content>
  49. </xml>
  50. ";
  51. echo sprintf($str, $toUser, $fromUser, $time, $content);
  52. }
  53. private function ImgText($toUser, $fromUser, $time, $title, $description, $picurl, $url)
  54. {
  55. $str = "<xml>
  56. <ToUserName><![CDATA[%s]]></ToUserName>
  57. <FromUserName><![CDATA[%s]]></FromUserName>
  58. <CreateTime>%s</CreateTime>
  59. <MsgType><![CDATA[news]]></MsgType>
  60. <ArticleCount>1</ArticleCount>
  61. <Articles>
  62. <item>
  63. <Title><![CDATA[%s]]></Title>
  64. <Description><![CDATA[%s]]></Description>
  65. <PicUrl><![CDATA[%s]]></PicUrl>
  66. <Url><![CDATA[%s]]></Url>
  67. </item>
  68. </Articles>
  69. </xml>
  70. ";
  71. echo sprintf($str, $toUser, $fromUser, $time, $title, $description, $picurl, $url);
  72. }
  73. }
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