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. }

更多相关文章

  1. 实现原生微信接入 当用户关注/发送消息时对应内容回应
  2. 使用easywechat完成微信接入,并且实现对接收不同消息的回复
  3. EasyWeChat的简单使用
  4. 作业内容:oop基础:请举例实例演绎以下难点 1. 类(对象抽象化的结
  5. 【前端 · 面试 】HTTP 总结(二)—— HTTP 消息
  6. 系统控制—清除历史消息
  7. CSS_0702作业
  8. VSCODE的安装配置与插件安装及HTTP协议初识
  9. python抓取微信撤回消息

随机推荐

  1. Android 打开浏览器的几种方法
  2. Android之圆角矩形
  3. 科普篇
  4. Android app版本号的修改
  5. 【Android學習專題】数据存储篇:SQLite
  6. 2011.09.13——— android Bitmap.Config
  7. Android ListView简单实用
  8. android send key event
  9. android 学习链接大集合
  10. Android ListView(Selector 颜色)