简体   繁体   English

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

[英]Send Bulk SMS via Twilio Rest API with PHP

I am using Twilio PHP Library , and i want to send SMS messages to a big list of people using Twilio PHP REST API. 我正在使用Twilio PHP库 ,我想使用Twilio PHP REST API向一大堆人发送短信。 I have a text file, that contains the list of unique phone numbers (approx 1000 members). 我有一个文本文件,其中包含唯一电话号码列表(约1000个成员)。

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. 目前我的计划是创建一个cron作业,它解析文本文件,并为文件中的每个电话号码进行REST API调用。

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). 但我想知道,Twilio是否可以为我做这项工作(就像我将文本文件上传到Twilio,Twilio将处理文件并向其中的每个号码发送短信)。

Is there any option like this with Twilio? 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? 这怎么可能与Twilio? Please let me know. 请告诉我。

EDIT: 27-09-2017: I have heard about Twilio COPILOT , but not sure how to use that service. 编辑:27-09-2017:我听说过Twilio COPILOT ,但不知道如何使用该服务。

Twilio developer evangelist here. Twilio开发者传道者在这里。

Just to distill your problem, you need to send about 1000 unique messages to users, right? 只是为了提炼你的问题,你需要向用户发送大约1000条独特的消息,对吧?

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 REST API进行API调用以单独发送每条消息 Twilio caps the message sending at 1 message per number per second. Twilio限制每秒每个号码发送一条消息的消息。 If you are already over this limit, then Twilio will queue those messages up for you. 如果您已经超过此限制,那么Twilio将为您排队这些消息。 It should not take 5 seconds per API call. 每个API调用不应该花费5秒。

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. 对于1000条消息,发送它们将花费将近17分钟。

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). 消息服务是由Copilot提供的数字池和其他服务(包括地理匹配号码,字母数字发送者后备和其他此类事物)。 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. 对于发送大量消息,例如在这种情况下,数字池是重要的部分,因为您发送的每个Twilio号码每秒可以发送1条消息。 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. 因此,如果您向消息传递服务添加2个号码,则每秒可以发送2条消息,如果向池中添加10个号码,则可以每秒发送10个号码。

To send messages using a messaging service and number pool, like this, you need to create a messaging service in your Twilio console . 要使用消息传递服务和数字池发送消息,您需要在Twilio控制台中创建消息传递服务 Add or buy numbers for the pool. 添加或购买游泳池的号码。 Then, get the messaging service SID: 然后,获取消息传递服务SID:

在创建服务的Twilio控制台中查找消息传递服务SID。

You can then use the messaging service SID in place of the from number in your call to the API. 然后,您可以使用消息传递服务SID代替API调用中的from号码。

$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. 值得注意的是,如果您需要在一天内发送1000条消息,我们建议您在至少4个数字上传播这些消息,因为美国运营商开始阻止使用超过该代码的长代码。 Check out more in our guidelines for sending SMS messages to the US . 有关向美国发送短信指南,请查看更多信息

Let me know if that helps at all. 如果这有帮助,请告诉我。

Very simple. 很简单。 First configure your number properly for notification and then use my code: 首先正确配置您的号码以进行通知,然后使用我的代码:

$message = 'Any text message';
$to = array();
foreach ($users as $user) { 
    $to[] = '{"binding_type":"sms", "address":"'.$user->phone_number.'"}';
}

$sid    = 'TWILIO_ACCOUNT_SID';
$token  = 'TWILIO_AUTH_TOKEN';
$services_id = 'TWILIO_SERVICE_ID';
$twilio = new Client($sid, $token);


$notification = $twilio
->notify->services($services_id)
->notifications->create([
    "toBinding" => $to,
    "body" => $message
]);

do you have the ability to create an SQL connection? 你有能力创建一个SQL连接? 1 column to house the number to call, one column for the message content? 1列容纳要呼叫的号码,一列是否为消息内容?

you can then loop through the rows in the table with your script above to fire off your messages one at a time. 然后,您可以使用上面的脚本遍历表格中的行,一次一个地触发您的消息。

//spaghetti code, will not work
//1. Create MySQL Connection
//2. loop through table
foreach($row as $number=>$message) {
$sms_sent = $client->messages->create(
    $number,
    array(
        'from' => '+1xxxxxxxxxx',
        'body' => $message,
        'statusCallback' => "http://myapplication_callback_url"
        )
    );
}

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

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