繁体   English   中英

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

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

我正试图通过twilio Api发送批量短信。 是否有任何方法可以在单个API请求中传递所有电话号码的数组。

Twilio开发者传道者在这里。

是的,现在有! 它被称为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"
    ]);

有关所有详细信息,请查看使用Notify passthrough API发送多条消息的文档

如果有其他人像我一样从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
]);
?>

首先,您需要正确配置twilio号码以进行通知。 然后您使用下面的代码发送批量短信。

$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