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条)

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊