首页 > 解决方案 > 如何通过 php 中的 CURL 使用 twillo 通知 API 发送批量消息?

问题描述

我想使用 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}

你们能帮帮我吗?

谢谢

标签: twiliotwilio-apitwilio-php

解决方案


Twilio 开发人员布道者在这里。

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

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

希望有帮助。


推荐阅读