我正在尝试通过twilio Api发送批量短信。是否有任何方法可以在单个API请求中传递所有电话号码的数组。
答案 0 :(得分:2)
Twilio开发者传道者在这里。
是的,现在有!它被称为passthrough API(因为它允许您通过许多不同的邮件系统并发送批量邮件。它是Notify API的一部分,您可以使用它发送批量短信。您需要在控制台中设置短信服务和通知服务,然后您可以使用以下代码:
<?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。
答案 1 :(得分:0)
如果有其他人像我一样从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
]);
?>
答案 2 :(得分:0)
首先,您需要正确配置您的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
]);