博客列表 >easywechat实现微信接入并不同消息回复+根据项目提供数据字典,对应建库建表,练习SQL语句。

easywechat实现微信接入并不同消息回复+根据项目提供数据字典,对应建库建表,练习SQL语句。

麦兜的故事
麦兜的故事原创
2021年09月04日 13:31:28970浏览

1、使用easywechat完成微信接入,并且实现对接收不同消息的回复。

  1. <?php
  2. include './vendor/autoload.php';
  3. use EasyWeChat\Factory;
  4. use EasyWeChat\Kernel\Messages\Text;
  5. use EasyWeChat\Kernel\Messages\News;
  6. use EasyWeChat\Kernel\Messages\NewsItem;
  7. $config = [
  8. 'app_id' => 'wx705529471efe4bf8',
  9. 'secret' => '211da643597922a04ff9e1fca224af1d',
  10. 'token' => 'phplove111',
  11. 'response_type' => 'array',
  12. //...
  13. ];
  14. $app = Factory::officialAccount($config);
  15. // $app->auto_reply->current();
  16. $app->server->push(function ($message) {
  17. switch ($message['MsgType']) {
  18. case 'text':
  19. if (preg_match('/您好/S', $message['Content'])) {
  20. return new Text('客服在线时间08:00~18:00');
  21. } else {
  22. $items = [
  23. new NewsItem([
  24. 'title' => '【普洱茶】降糖,居然让我们越喝越胖!',
  25. 'description' => '你们去年磕的cp,今年已经凉透了,但是无糖饮料却从去年火到了今年,降糖你还相信吗?',
  26. 'url' => 'https://baidu.com',
  27. 'image' => 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13906082969%2F1000&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633314966&t=10e1a37b4f41e20ded0b5278d7a04e7e',
  28. // ...
  29. ]),
  30. ];
  31. return new News($items);
  32. }
  33. case 'event':
  34. if ($message['Event'] == 'subscribe') {
  35. return '你这么好看,关注我不迷路!';
  36. }
  37. case 'image':
  38. return '什么图片,放大我看看';
  39. }
  40. });
  41. // $text = new Text('您好!overtrue。');
  42. $response = $app->server->serve();
  43. // 将响应输出
  44. $response->send();
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议