This time to bring you php+mysql implementation of Message Queuing steps in detail, php+mysql implementation of Message Queuing considerations, the following is the actual case, together to see.
Recently encountered a demand for bulk SMS, SMS interface is provided by a third party. Just started to think, get to the mobile phone number, the loop call interface send it is OK?
But soon the problem was found: when the number of messages is large, it is not only time consuming, but also a low success rate.
So I think, PHP and MySQL to implement a message queue, one by one to send text messages. Here are the specific implementations:
First, create a data table SMS that contains the following fields:
Id,phone,//Mobile number content//SMS Contents
Save the SMS and phone number you want to send into the SMS table.
Next, you need to implement a timer in PHP, read a record regularly, and send a text message:
<?php$db = new db (), $sms = new SMS (), while (true) { $item = $db->getfirstrecord ();//Get data table first record if (! $item) { //If there is no data in the queue, end the timer break ; } $res = $sms->send ($item [' phone '], $item [' content ']; Send SMS if ($res) { $db->deletefristrecord ();//delete send successful record echo $item [' phone ']. ' Send success '; } else{ echo $item [' phone ']. ' Send failed, continue at a later time '; } Sleep (10);//cycle once every 10 seconds}echo ' send complete! ';? >
Save the code as timer_sms.php, open the command line, and execute the timer:
PHP timer_sms.php
Well, the PHP timer will automatically complete the task of sending text messages based on the set interval (10 seconds). When the task is completed, the timer is automatically exited and server resources are no longer occupied.
According to my test, the PHP timer takes up a lot of resources and does not cause any stress to the server. It also accesses the database asynchronously and does not affect the operation of the database.
The advantages of this approach are:
1, the backstage operation, the front desk does not need to wait
2, the success rate is high, the record of failure will automatically re-send, until successful
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
Use Vue to implement the drag and drop function step
node to make a personal number robot steps