0

The Twilio Passthrough API or Notify service is supposed to allow you to send SMS (or Facebook Messenger, WhatsApp, etc.) messages in bulk with a single API call. However, I'm having difficulty getting the call and Twilio's toBindings attribute to accept an array of values.

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

In the above example it is only sending the first SMS. It is not cycling through the array given.

Twilio support sent me this example:

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

and indeed it works as presented. But what is the point of using an array of values if you have to explicitly declare each and every array key in the attributes? Have even tried with their example:

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

and it still will only send the first SMS. What am I missing here?

Data Do IT
  • 11
  • 4

1 Answers1

2

You're double-arraying things:

"toBinding" => [ $toBindingAttributes ],

$toBindingAttributes is already an array, so:

"toBinding" => $toBindingAttributes,

should do the trick.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • Side note: I have no idea why Twilio's support is doing `"toBinding" => [$MyNumbers[0],$MyNumbers[1]],` instead of just `"toBinding" => $MyNumbers,`. Weird. You might consider replying with feedback on their snippet. – ceejayoz Dec 11 '18 at 16:35
  • That is precisely it! Thank you so much. Will inform Twilio. – Data Do IT Dec 11 '18 at 17:01