如何在没有 for 循环的情况下在 twilio api 中发送群发短信
How to send bulk sms in twilio api without for loop
我正在尝试通过 twilio Api 发送群发短信。有什么方法可以在单个 API 请求中传递所有 phone 数字的数组。
此处为 Twilio 开发人员布道师。
是的,现在有!它被称为 passthrough API (as it allows you to pass through many different messaging systems and send bulk messages. It's part of the 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"
]);
结帐the documentation on sending multiple messages with the Notify passthrough API for all the details。
如果其他人像我一样在从 PHP array() 准备 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
]);
我正在尝试通过 twilio Api 发送群发短信。有什么方法可以在单个 API 请求中传递所有 phone 数字的数组。
此处为 Twilio 开发人员布道师。
是的,现在有!它被称为 passthrough API (as it allows you to pass through many different messaging systems and send bulk messages. It's part of the 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"
]);
结帐the documentation on sending multiple messages with the Notify passthrough API for all the details。
如果其他人像我一样在从 PHP array() 准备 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
]);