RoBoDeveloper
RoBoDeveloper

Reputation: 31

Sending bulk messages in twillo notify API via CURL (php)

$data = [];
    $data['ToBinding'] =  json_encode(array("binding_type"=>"sms", "address"=>"+19991112222"));
    $data['Body'] ="test";
    $ch = curl_init("https://notify.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXX/Notifications");
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    
    curl_setopt($ch, CURLOPT_USERPWD,'XXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXX');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $resultData = curl_exec($ch);

This code was copied from another post. I am able to get this to work just fine, using real numbers of course. However I cannot populate $data['ToBinding'] with multiple numbers, which is the whole purpose of using Twilio Notify. I have tried many different combinations of code and it blows up, most of the time with "Cannot convert incoming parameters to notification object: Parameter 'ToBinding' is invalid".

I was able to get it to at least execute without errors using this code (real numbers of course):

$data['ToBinding'] =  json_encode(array("binding_type"=>"sms", "address"=>"+19991112222","binding_type"=>"sms", "address"=>"+19993334444"));

But it only sends to the first number in the array. Any help on how to populate the array to send to multiple numbers (or maybe another way using cURL) will be appreciated.

==== FULL CODE ====

$query = array("ToBinding" => array(
json_encode(array("binding_type"=>"sms", "address"=>"+19991112222")),
json_encode(array("binding_type"=>"sms", "address"=>"+19993334444"))
));
$data = http_build_query($query);
$data = preg_replace('/%5B[0-9]+%5D/simU', '', $data);
echo $data;
$data['Body'] ="Notify cURL API test";
$ch = curl_init("https://notify.twilio.com/v1/Services/<NOTIFY ID HERE >/Notifications");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    
curl_setopt($ch, CURLOPT_USERPWD,'<ACCT ID HERE>:<TOKEN HERE >');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resultData = curl_exec($ch);

===== FINAL WORKING CODE =====

$data['Body'] ="Notify cURL API test";
$data['ToBinding'] = array(
   json_encode(array("binding_type"=>"sms","address"=>"+19191112222")),
   json_encode(array("binding_type"=>"sms","address"=>"+19193334444"))
);

$query = http_build_query($data);
$string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query);
$ch = curl_init("https://notify.twilio.com/v1/Services/ISxxxxxxxxxx/Notifications");
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    
curl_setopt($ch, CURLOPT_USERPWD,'ACxxxxxxxxxx:xxxxxxxxxxxxxxxxx');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resultData = curl_exec($ch);

echo "curl Response=".$resultData."<br>";
$responseHttp = curl_getinfo($ch, CURLINFO_HTTP_CODE);

Upvotes: 3

Views: 937

Answers (1)

philnash
philnash

Reputation: 73047

Twilio developer evangelist here.

The ToBinding parameter is an array of binding objects. Notify implements support for that by decoding multiple ToBinding parameters from the request.

The curl example from the Notify documentation looks like this:

curl -X POST https://notify.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications \
    --data-urlencode 'ToBinding={"binding_type":"sms", "address":"+15555555555"}' \
    --data-urlencode 'ToBinding={"binding_type":"facebook-messenger", "address":"123456789123"}' \
    -d 'Body=Hello Bob' \
    -u 'your_account_sid:your_auth_token'

As you can see, there are two ToBinding parameters included in the data.

As far as I can tell, PHP doesn't support building the body like that. http_build_query appears to be useful, but builds arrays of data using name[index] form, which we don't want. You can strip the [index] out though, with something like the following:

$query = array("ToBinding" => array(
  json_encode(array("binding_type"=>"sms", "address"=>"+19991112222")),
  json_encode(array("binding_type"=>"sms", "address"=>"+19993334444"))
));
$data = http_build_query($query);
$data = preg_replace('/%5B[0-9]+%5D/simU', '', $data);
echo $data;
# => ToBinding=%7B%22binding_type%22%3A%22sms%22%2C%22address%22%3A%22%2B19991112222%22%7D&ToBinding=%7B%22binding_type%22%3A%22sms%22%2C%22address%22%3A%22%2B19993334444%22%7D

Let me know if this helps at all.

Upvotes: 1

Related Questions