dongniaocheng3773 2017-09-21 15:27
浏览 167
已采纳

使用PHP通过Twilio Rest API发送批量短信

I am using Twilio PHP Library, and i want to send SMS messages to a big list of people using Twilio PHP REST API. I have a text file, that contains the list of unique phone numbers (approx 1000 members).

Currently my plan was to create a cron job, which parses the text file, and makes a REST API call for each of the phone numbers in the file.

Ex:

<?php
require_once 'vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new Client($sid, $token);

$sms_sent = $client->messages->create(
    '+1xxxxxxxxxx',
    array(
        'from' => '+1xxxxxxxxxx',
        'body' => "XXXXXX XXXXXX",
        'statusCallback' => "http://myapplication_callback_url"
    )
);

But i wanted to know, whether Twilio can do the job for me (like i will upload the text file to Twilio, and Twilio will process the file and send SMS to each number in it).

Is there any option like this with Twilio? I have read about the BULK SMS option here, but that seems like it sends out a static message to all the users. I need to send unique message to each user. How is this possible with Twilio? Please let me know.

EDIT: 27-09-2017: I have heard about Twilio COPILOT, but not sure how to use that service.

  • 写回答

3条回答 默认 最新

  • duannue2455 2017-09-28 12:39
    关注

    Twilio developer evangelist here.

    Just to distill your problem, you need to send about 1000 unique messages to users, right?

    If that's the case, then this is what you need to do.

    Since the message is unique to each number, you need to make an API call to the Twilio REST API to send each message individually. Twilio caps the message sending at 1 message per number per second. If you are already over this limit, then Twilio will queue those messages up for you. It should not take 5 seconds per API call.

    This is the case if you use one number, like in your original example. For 1000 messages it will take almost 17 minutes to send them all.

    However, you can use a messaging service to speed this up. A messaging service is a pool of numbers and other services provided by Copilot (including geo matching numbers, alphanumeric sender fallback and other such things). For sending a lot of messages, like in this case, the number pool is the important part because each Twilio number you are sending from can send 1 message per second. So if you add 2 numbers to the messaging service you can send 2 messages per second, if you add 10 numbers to the pool you can send 10 a second.

    To send messages using a messaging service and number pool, like this, you need to create a messaging service in your Twilio console. Add or buy numbers for the pool. Then, get the messaging service SID:

    Find the messaging service SID in your Twilio console where you created the service.

    You can then use the messaging service SID in place of the from number in your call to the API.

    $sms_sent = $client->messages->create(
        '+1xxxxxxxxxx',
        array(
            'from' => 'MESSAGING SERVICE SID',
            'body' => "XXXXXX XXXXXX",
            'statusCallback' => "http://myapplication_callback_url"
        )
    );
    

    Then your messages will be fanned out by the messaging service.

    Notably, if you need to send 1000 messages in a day, we recommend you spread that out over at least 4 numbers as US carriers start to block long code numbers that are used more than that. Check out more in our guidelines for sending SMS messages to the US.

    Let me know if that helps at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?