简体   繁体   English

使用 Twilio PHP 库向 2,000 多个号码发送批量 SMS

[英]Send a bulk SMS using Twilio PHP library to 2,000+ numbers

I have an integration with Twilio's PHP library to bulk send SMS messages.我与 Twilio 的 PHP 库集成以批量发送 SMS 消息。 This is all working fine with 5-10 numbers, however, I keep getting a 504 timeout error after 60 seconds when sending to hundreds of numbers.这一切都适用于 5-10 个数字,但是,当发送到数百个数字时,我在 60 秒后不断收到 504 超时错误。 I have created a simple form that you can type a message into which submits to Twilio's API.我创建了一个简单的表单,您可以在其中输入一条消息,将其提交给 Twilio 的 API。 My integration opens a CSV which contains all the numbers and sends the message to each number.我的集成打开了一个 CSV ,其中包含所有号码并将消息发送到每个号码。 The issue I have is that after 60 seconds I hit a 504 timeout error and therefore only 200-300 numbers receive my SMS.我遇到的问题是,60 秒后我遇到了 504 超时错误,因此只有 200-300 个号码收到我的短信。 How can I avoid this?我怎样才能避免这种情况? Is there a way to break down the nubmers into batches?有没有办法将数字分解成批次?

<?php
require __DIR__ . '/src/Twilio/autoload.php';

use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'xxxxxxxxxxxxxxx';
$token = 'xxxxxxxxxxxxx';
$notify_sid = 'xxxxxxxxxxxx';
$client = new Client($sid, $token);

    $row = 1;
        if (($handle = fopen("test.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 100, ",")) !== FALSE) {
                $num = count($data);
                $row++;
                    for ($c=0; $c < $num; $c++) {
                        $numbersFinal = "'".$data[$c] . "',";
                        
                    if ($_SERVER["REQUEST_METHOD"] == "POST") {

                        // Use the client to do fun stuff like send text messages!
                        $message = $_POST['message'];

                        $phoneNumbers = array($numbersFinal);

                        $to = array();
                        foreach ($phoneNumbers as $phoneNumber) { 
                            $to[] = '{"binding_type":"sms", "address":"'.$phoneNumber.'"}';
                        }

                        $request_data = [
                            "toBinding" => $to,
                            'body' => $message
                        ];


                        // Create a notification
                        $notification = $client
                        ->notify->services($notify_sid)
                        ->notifications->create($request_data);

                        }

                    }
                }
    
            fclose($handle);
        }

    ?>

504 is a timeout error. 504 是超时错误。 This error occurs because of your php.ini settings.由于您的 php.ini 设置,会发生此错误。 Please check the phpinfo() output.请检查phpinfo() output。 Look for max_execution_time variable.寻找max_execution_time变量。 It's probably 60 seconds.大概是60秒。 While you run the file, server terminates the process after this time exceeded.当您运行文件时,服务器会在超过此时间后终止进程。

To handle this problem, you can follow several ways:要处理此问题,您可以遵循以下几种方法:

  1. Ask your service provider to raise this limit.要求您的服务提供商提高此限制。 Usually, they reject those requests.通常,他们会拒绝这些请求。
  2. You can run this file as a cron task.您可以将此文件作为 cron 任务运行。 When you click send button, you can save it as a cronjob.单击发送按钮时,可以将其保存为 cronjob。 You can figure it out.你可以弄清楚。
  3. When you click send, you can send sms by 100, 100,... send_sms.php?sent=100 could send 100 sms then redirect to send_sms.php?sent=200... until they end.单击发送时,您可以通过 100、100、... send_sms.php?sent=100 发送 100 条短信然后重定向到 send_sms.php?sent=200... 直到结束。

Tell me if any of these solutions work for you.告诉我这些解决方案是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM