This article mainly explains how to send template messages to WeChat users who are about to expire and remind them to renew. It mainly shares with you how to send WeChat template messages through swoole multi-process in PHP. I hope it can help everyone.
First of all, we get the users who are about to expire, ranging from 800 to 2000 every day. I feel that the pressure is not very great, so I directly foreach the array and then send it. The problem of request timeout, which is 502, often occurs, and then the operation colleague raised the question In order to push template messages to a large number of users, we launched a wave of promotions a year ago, and the volume was around 10,000 per day, so we immediately created an upload function for operators to directly upload and send, and then used the unlimited 502bad way.
At this time, I feel that the number of template messages has increased and needs optimization, so I adopt another solution, redis queue + daemon process, to decouple the uploaded data from the sent data, and the daemon process detects the redis queue Is there any data that needs to be sent? If so, start fetching it out one by one and then send it. This solution feels like the operator does not need to worry about the results and will definitely be able to send it successfully, but it will take a long time and it will take a long time to finish sending if the volume is large. So Another problem arises. The time for sending is not used. I want to send it to the user during the peak time when the user opens WeChat. So continue to optimize!
At this time, use php+swoole for multi-process asynchronous processing. , since one process is slow, let's open a few more, so 16 processes are sent asynchronously together. 10,000 template messages are sent within 10 minutes, and the basic transformation is completed.
First start server.php
Client
class Client { public function send($msg){ $client = new swoole_client(SWOOLE_SOCK_TCP); //连接到服务器 if (!$client->connect('127.0.0.1', 9501, 0.5)) { $this->write("connect failed."); } //向服务器发送数据 if (!$client->send($msg)) { $this->write("send ".$msg." failed."); } //关闭连接 $client->close(); } private function write($str){ $path = "/sys.log"; $str = "[".date("Y-m-d H:i:s")."]".$str; $str .= PHP_EOL; file_put_contents($path,$str,FILE_APPEND); } }
Server
<?php $serv = new swoole_server("127.0.0.1", 9501); //设置异步任务的工作进程数量 $serv->set(array('task_worker_num' =>16)); //监听数据接收事件 $serv->on('receive', function($serv, $fd, $from_id, $data) { //投递异步任务 $task_id = $serv->task($data);//非阻塞 echo "同步代码执行完成\n"; }); //处理异步任务 $serv->on('task', function ($serv, $task_id, $from_id, $data) { handleFun($data); //返回任务执行的结果 $serv->finish("finish"); }); //处理异步任务的结果 $serv->on('finish', function ($serv, $task_id, $data) { echo "异步任务执行完成"; }); $serv->start(); function handleFun($data){ $data=json_decode($data,true); foreach ($data as $key => $value) { echo json_encode($value); $url="xxxx";//调用发送模板消息接口,服务端没办法直接获取微信的接口的一些数据,此处做了一些加密 $postUrl = $url; $curlPost = $value; $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页 curl_setopt($ch, CURLOPT_HEADER, 0); //设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1); //post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch); //运行curl curl_close($ch); } }
Calling client.php
<?php include dirname(__FILE__).'/client.php'; $params=""//接口数据 $msg = json_encode($params); $client = new Client();$client->send($msg);echo "[".date("Y-m-d H:i:s")."]执行完成".PHP_EOL;
Related recommendations:
How to add swoole automatically to phpstorm Tips
thinkphp5 and swoole asynchronous mass mailing implementation method
Swoole implements WeChat code scanning login function
The above is the detailed content of PHP's swoole multi-process sends WeChat template messages. For more information, please follow other related articles on the PHP Chinese website!