Joke Collection Website - Public benefit messages - PHP+MYSQL realizes how to send short messages in queue.
PHP+MYSQL realizes how to send short messages in queue.
But soon found the problem: when the number of short messages is large, it is not only time-consuming, but also the success rate is very low.
So I thought of using PHP and MySQL to realize a message queue and send text messages one by one. The following is the specific implementation method:
First, establish an sms data table, which contains the following fields:
id,
Phone,//phone number
Content//SMS content
Store the short messages and mobile phone numbers that need to be sent in the short message table.
Next, you need to implement a timer in PHP to periodically read records and send a short message:
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
$ Db = new Db();
$ Sms = new Sms();
while(true){
$ item = $ d B- & gt; get first record(); //Get the first record in the data table
If (! $item){
//If there is no data in the queue, end the timer.
Break;
}
$ RES = $ SMS-& gt; Send($item[' phone'], $item[' content']); //Send SMS
if($res){
$ d B- & gt; deleteFristRecord(); //Delete the record of successful sending.
Echo $item['phone']。 "Sending succeeded";
} Otherwise {
Echo $item['phone']。 Sending failed, please try again later';
}
Sleep (10); //Cycle once every ten seconds.
}
Echo "Send complete!" ;
& gt
Save the code as timer_sms.php, open the command line and execute the timer:
Php timer _sms.php
Ok, the php timer will automatically complete the task of sending short messages according to the set time interval (here, 10 second). After the task is completed, it will automatically exit the timer and no longer occupy the server resources.
According to my test, PHP timer does not occupy too many resources and will not put pressure on the server. Moreover, accessing the database is asynchronous and will not affect the operation of the database.
The advantages of this method are:
1, running in the background, without waiting at the front desk.
2. The success rate is high, and the failed records will be automatically retransmitted until they are successful.
- Related articles
- Where is Fujian Welfare Lottery Center?
- How to recover Google account?
- Supplementary announcement on the written test of the qualification examination for primary and secondary school teachers in Hebei Province in the first half of 2022?
- How to open the paid SMS permission function
- Aries loses his temper when he sees his predecessor's text message.
- Looking for an ancient Chinese sentence, it is a man's sentence to his wife who returned to her parents' home, telling her to come back. . .
- How should I send a text message to greet my family when they are sick?
- How to include the URL in the short message content?
- What does copywriting mean?
- Does Jianyang Rural Commercial Bank have a ten-year mortgage?