如何使用twillo通知API通过php中的CURL发送批量消息?

时间:2018-12-21 16:03:43

标签: twilio twilio-api twilio-php

我想使用twillio通知API使用php中的CURL发送批量消息,我正在尝试以下代码:

$data = [];
    $data['ToBinding'] =  array("binding_type"=>"sms", "address"=>"+12013318779");
    $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);

我想我在CURLOPT_POSTFIELDS上做错了什么,但是我尝试了每件事都改变了它,但是每次我得到以下响应时:

{"code": 20001,
"message": "At least one parameter among Identity, Tag, and ToBinding must be specified",
"more_info": "https://www.twilio.com/docs/errors/20001",
"status": 400}

你们能帮我吗?

谢谢

1 个答案:

答案 0 :(得分:0)

这里是Twilio开发人员的传播者。

ToBinding要求将数据编码为JSON。请尝试以下操作:

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

希望有帮助。