基于redis+mysql+php的简单微信服务号通知队列

本文阅读 1 分钟
首页 PHP笔记 正文

Queue.php

  1. <?php
  2. /**
  3. * Queue.php
  4. * User: Administrator
  5. * Date: 2019/4/4
  6. * Time: 13:19
  7. */
  8. namespace Index;
  9. require_once('Weixin/Wxapi.php');
  10. require_once('Mysql/Mysql.php');
  11. use Weixin\Wxapi;
  12. use Mysql\Mysql;
  13. class Index {
  14. private $Redis;
  15. private $Expire;
  16. private $WxData;
  17. private $DB;
  18. public function __construct()
  19. {
  20. $this->WxData = [
  21. //服务号1
  22. "a" => [ "AppId" =>"",
  23. "AppSecret" =>"",
  24. "Token" =>"",
  25. "Crypt" =>"",
  26. "template" =>""
  27. ],
  28. //服务号2
  29. "b" => [
  30. "AppId" => "",
  31. "AppSecret" => "",
  32. "Token" =>"",
  33. "Crypt" =>"",
  34. "template" => ""
  35. ],
  36. "c" => [
  37. "AppId" => "",
  38. "AppSecret" => "",
  39. "Token" =>"",
  40. "Crypt" =>"",
  41. "template" => ""
  42. ]
  43. ];
  44. $this ->Redis = new \Redis();
  45. $this ->Redis -> connect( "127.0.0.1", "6379" );
  46. $this->DB = new Mysql();
  47. // $this ->Redis ->auth("123456");
  48. self::init();
  49. }
  50. public function init(){
  51. while (true) {
  52. $key = $this->Redis->keys("*");
  53. if( !$key ){
  54. sleep(10);
  55. }else{
  56. foreach ($key as $k) {
  57. $value = $this->Redis->lpop($k);
  58. $data = json_decode($value, true);
  59. if ($data) {
  60. $AppId = $this->WxData[$k]["AppId"];
  61. $AppSecret = $this->WxData[$k]["AppSecret"];
  62. $templateId = $this->WxData[$k]["template"];
  63. // echo $AppId;
  64. $Wxapi = new Wxapi($AppId, $AppSecret);
  65. // var_dump($data);
  66. $url = "http://www.qq.com";
  67. if ($data["send_type"] == "custom") {
  68. $send = [
  69. "type" => "news", //类型
  70. "title" => $data["title"], //消息标题
  71. "description" => "点击进入查看详情", //图文详情
  72. "picurl" => "http://www.abc.com/abc.jpg",
  73. "url" => $url,
  74. ];
  75. $res = $Wxapi->send_custom_message($data["wx_openid"], $send);
  76. $type = 1;
  77. }
  78. $ret = json_decode($res, true);
  79. if ($data["send_type"] == "template" or $ret["errcode"] !=0 ) {
  80. $template = [
  81. "first" => [
  82. "value" => $data["title"],
  83. "color" => "#ff0000"
  84. ],
  85. "keyword1" => [
  86. "value" => "服务消息",
  87. "color" => "#ff0000"
  88. ],
  89. "keyword2" => [
  90. "value" => date('Y-m-d H:i'),
  91. "color" => "#173177"
  92. ],
  93. "remark" => [
  94. "value" => "点击进入查看详情",
  95. "color" => "#173177"
  96. ],
  97. ];
  98. $res = $Wxapi->send_template_message($data["wx_openid"], $templateId, $template, $url);
  99. $type = 2;
  100. }
  101. $ret = json_decode($res, true);
  102. $log = [
  103. "user_id" => $data["id"],
  104. "tradelist_id" => $data["tradelist_id"],
  105. "room_id" => $data["room_id"],
  106. "log" => $res,
  107. "errcode" => $ret["errcode"],
  108. "add_time" => time(),
  109. "type" => $type,
  110. ];
  111. $this->DB->insert("sz_queue_log", $log);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. $obj = new Index();

mysql.php

  1. <?php
  2. /**
  3. * Mysql.php
  4. * User: Administrator
  5. * Date: 2019/4/4
  6. * Time: 13:19
  7. */
  8. namespace Mysql;
  9. class Mysql
  10. {
  11. public $DB;
  12. //构造函数,获取Access Token
  13. public function __construct()
  14. {
  15. $dbms = 'mysql'; //数据库类型
  16. $host = '127.0.0.1'; //数据库主机名
  17. $dbName = '127.0.0.1'; //使用的数据库
  18. $user = 'root'; //数据库连接用户名
  19. $pass = '123456'; //对应的密码
  20. $dsn = "$dbms:host=$host;dbname=$dbName";
  21. return self::initConnect($dsn, $user, $pass);
  22. }
  23. protected function initConnect($dsn, $user, $pass){
  24. if ( !$this->DB ) $this->DB = $this->connect($dsn, $user, $pass);
  25. return $this->DB;
  26. }
  27. public function connect($dsn, $user, $pass){
  28. $this->DB = new \PDO($dsn, $user, $pass, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
  29. try
  30. {
  31. $this->DB->query("SELECT 1");
  32. echo "ok";
  33. }
  34. catch (\PDOException $e){
  35. $this->DB = new \PDO($dsn, $user, $pass, array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
  36. }
  37. return $this->DB;
  38. }
  39. /**
  40. * 插入数据
  41. * @param $table 数据表
  42. * @param $data 数据数组
  43. * @return mixed 插入ID
  44. */
  45. public function insert($table, $data)
  46. {
  47. foreach ($data as $key => $value) {
  48. $data[$key] = $value;
  49. }
  50. $keys = '`' . implode('`,`', array_keys($data)) . '`';
  51. $values = '\'' . implode("','", array_values($data)) . '\'';
  52. $sql = "INSERT INTO {$table}( {$keys} )VALUES( {$values} )";
  53. $this->DB->query($sql);
  54. return $this->mysqli->insert_id;
  55. }
  56. }

Wxapi.php

  1. <?php
  2. /**
  3. *Wxapi.php.
  4. * User: Administrator
  5. * Date: 2019/4/4
  6. * Time: 10:15
  7. */
  8. namespace Weixin;
  9. class Wxapi
  10. {
  11. public $appid ;
  12. public $appsecret ;
  13. public $WxData;
  14. //构造函数,获取Access Token
  15. public function __construct($Appid = NULL, $Appsecret = NULL)
  16. {
  17. if($Appid && $Appsecret){
  18. $this->appid = $Appid;
  19. $this->appsecret = $Appsecret;
  20. // echo $this->appid ."[]". $this->appsecret ;
  21. //3. 本地写入
  22. $res = file_get_contents('/www/Queue/Weixin/json/'.$Appid.'.json');
  23. $result = json_decode($res, true);
  24. $this->expires_time = $result["expires_time"];
  25. $this->access_token = $result["access_token"];
  26. if (time() > ($this->expires_time + 7000)){
  27. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
  28. // file_put_contents('./url.php',$url);
  29. $res = $this->http_request($url);
  30. $result = json_decode($res, true);
  31. $this->access_token = $result["access_token"];
  32. $this->expires_time = time();
  33. file_put_contents('/www/Queue/Weixin/json/'.$Appid.'.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}');
  34. }
  35. }
  36. }
  37. //发送模版消息
  38. public function send_template_message($touser,$template_id,$data,$url){
  39. $msg = array('touser' =>$touser);
  40. $msg["template_id"] = $template_id;
  41. $msg["topcolor"] ="#FF0000";
  42. $msg["data"] = $data;
  43. $msg["url"] = $url;
  44. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token;
  45. return $this->http_request($url, urldecode(json_encode($msg)));
  46. }
  47. //发送客服消息
  48. public function send_custom_message($touser, $data)
  49. {
  50. $msg = ['touser' =>$touser,
  51. 'msgtype' => "news",
  52. 'news' => [
  53. "articles" => [$data],
  54. ]
  55. ];
  56. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
  57. return $this->http_request($url, urldecode(json_encode($msg,320)));
  58. }
  59. //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
  60. protected function http_request($url, $data = null)
  61. {
  62. $curl = curl_init();
  63. curl_setopt($curl, CURLOPT_URL, $url);
  64. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  65. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  66. if (!empty($data)){
  67. curl_setopt($curl, CURLOPT_POST, 1);
  68. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  69. }
  70. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  71. $output = curl_exec($curl);
  72. curl_close($curl);
  73. return $output;
  74. }
  75. }
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

PHP原生socket与Websocket握手、解码、加码
« 上一篇 04-01
价值1200元的抖音“矩阵营销”课程(持续更新)
下一篇 » 04-10

发表评论

1 条回复
  1. 文章不错非常喜欢

惪特博客
  • 文章总数:
    18496 篇
  • 评论总数:
    53290 条
  • 标签总数:
    8869 个
  • 总浏览量:
    22304858 次
  • 最后更新:
    5天前

最多点赞

随便看看

标签TAG