在twillo中发送大量消息通过CURL(php)通知API

问题描述 投票:-1回答:1
$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);

此代码是从另一篇文章中复制的。我当然可以使用实际数字使它正常工作。但是我不能用多个数字填充$ data ['ToBinding'],这是使用Twilio Notify的全部目的。我尝试了许多不同的代码组合,但大多数情况下,它用“无法将传入参数转换为通知对象:参数'ToBinding'无效'。”

使用此代码,我至少能够使它至少执行无误(当然是实数:]

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

但是它只会发送到数组中的第一个数字。任何有关如何填充数组以发送给多个数字(或使用cURL的另一种方式)的帮助都将不胜感激。

====完整代码====

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

=====最终工作代码=====

$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);
php api curl twilio notify
1个回答
0
投票

Twilio开发人员推广人员在这里。

ToBinding参数是绑定对象的数组。通知通过从请求中解码多个ToBinding参数来实现对此的支持。

curl example from the Notify documentation看起来像这样:

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'

如您所见,数据中包含两个ToBinding参数。

据我所知,PHP不支持那样构建主体。 http_build_query似乎很有用,但是我们不希望使用http_build_query形式构建数据数组。您可以通过以下方式删除name[index]

[index]

让我知道这是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.