PHP MySQL implements Message Queuing

Source: Internet
Author: User

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, the end 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 to try later ';} 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

PHP MySQL implements Message Queuing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.