简体   繁体   English

如何在没有for循环的情况下在twilio api中发送批量短信

[英]How to send bulk sms in twilio api without for loop

I'm trying to send bulk sms through twilio Api. 我正试图通过twilio Api发送批量短信。 Is there any method to pass the array of all phone numbers in a single API request. 是否有任何方法可以在单个API请求中传递所有电话号码的数组。

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

Yes there is now! 是的,现在有! It's known as the passthrough API (as it allows you to pass through many different messaging systems and send bulk messages. It's part of the Notify API and you can use it to send bulk SMS messages. You need to set up a messaging service and a notify service in your console, then you can use the following code: 它被称为passthrough API (因为它允许您通过许多不同的消息传递系统并发送批量消息。它是Notify API的一部分,您可以使用它来发送批量SMS消息。您需要设置消息传递服务和在您的控制台中通知服务,然后您可以使用以下代码:

<?php
// NOTE: This example uses the next generation Twilio helper library - for more
// information on how to download and install this version, visit
// https://www.twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php';

use Twilio\Rest\Client;

// Your Account SID and Auth Token from https://www.twilio.com/console
$accountSid = "your_account_sid";
$authToken = "your_auth_token";

// your notify service sid
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

// Initialize the client
$client = new Client($accountSid, $authToken);

// Create a notification
$notification = $client
    ->notify->services($serviceSid)
    ->notifications->create([
        "toBinding" => [
            '{"binding_type":"sms", "address":"+15555555555"}',
            '{"binding_type":"sms", "address":"+12345678912"}'
        ],
        "body" => "Hello Bob"
    ]);

Checkout the documentation on sending multiple messages with the Notify passthrough API for all the details . 有关所有详细信息,请查看使用Notify passthrough API发送多条消息的文档

In case someone else had troubles with preparing the toBinding parameter from a PHP array() as I had, here is an example for that: 如果有其他人像我一样从PHP数组()准备toBinding参数时遇到麻烦,这里有一个例子:

<?php
require_once '/path/to/vendor/autoload.php';

use Twilio\Rest\Client;

$accountSid = "your_account_sid";
$authToken = "your_auth_token";
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

$client = new Client($accountSid, $authToken);

$recipients = array($num1, $num2, ...); // Your array of phone numbers

$binding = array();
foreach ($recipients as $recipient) { 
    $binding[] = '{"binding_type":"sms", "address":"+1'.$recipient.'"}'; // +1 is used for US country code. You should use your own country code.
}

$notification = $client
->notify->services($service_sid)
->notifications->create([
    "toBinding" => $binding,
    "body" => $text
]);
?>

First of all, you need to configure your twilio number properly for notification. 首先,您需要正确配置twilio号码以进行通知。 Then your to use below code to send bulk SMS. 然后您使用下面的代码发送批量短信。

$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
]);

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

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