如何使用Twilio通知将SMS消息发送给多个收件人?

时间:2018-12-11 16:30:10

标签: php twilio twilio-php

Twilio Passthrough API或通知服务应允许您通过单个API调用批量发送SMS(或Facebook Messenger,WhatsApp等)消息。但是,我很难让电话和Twilio的toBindings属性接受值数组。

$Addresses = array("+19999999999", "+18888888888");
$toBindingAttributes = array();

foreach ($Addresses as $Address) {
    array_push($toBindingAttributes, '{"binding_type":"sms","address":"' . $Address . '"}');
}

$notification = $client->notify->services($MyNotifySid)->notifications->create([
    "toBinding" => [ $toBindingAttributes ],
    "body" => "This is a manual test."
    ]);

在上面的示例中,它仅发送第一个SMS。它没有遍历给定的数组。

Twilio支持向我发送了此示例:

$MyNumbers = array('{"binding_type":"sms", "address":"+1555555555"}', '{"binding_type":"sms", "address":"+14444444444"}');
$notification = $client->notify->services($serviceSid)->notifications->create([
    "toBinding" => [$MyNumbers[0],$MyNumbers[1]],
    "body" => "Notification Test"
]);

,并且确实如介绍的那样工作。但是,如果必须在属性中显式声明每个数组键,那么使用值数组有什么意义呢?甚至尝试过他们的例子:

"toBinding" => [ implode(",", $MyNumbers) ],

,它仍然只会发送第一个SMS。我在这里想念什么?

1 个答案:

答案 0 :(得分:2)

您正在对事物进行双重排列:

"toBinding" => [ $toBindingAttributes ],

$toBindingAttributes已经 一个数组,所以:

"toBinding" => $toBindingAttributes,

应该可以解决问题。