使用CURL发送Twizo SMS

时间:2019-06-10 07:59:48

标签: php json sms

我一直在尝试使用来自https://www.twizo.com/developers/documentation/

的API指令发送短信
$data = array(
    'sender'=>'Me',
    'body'=>'Message',
    'recipients'=>'201*****0'
);

$string = http_build_query($data);
$ch = curl_init("https://twizo:API-KEY@api-asia-01.silverstreet.com/v1/sms/submitsimple");

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
print_r($server_output);

代码有效,但返回错误消息:

{“ validation_messages”:{“ recipients”:{“ noArraySupplied”:“此字段仅允许使用数组值”}},“ type”:“ http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html”,“ title”:“不可处理实体“,”状态“:422,”详细信息“:”验证失败“}

EDIT2: 上面的问题已解决,但现在我要进行此工作:

send.php文件:

set_time_limit(0);
if(isset($_POST['submit'])){
$letter = $_POST['message'];
$recs = $_POST['rec'];
$mailist = $_POST['number'];
$from = $_POST["from"];
$message = $letter;
$line = 0;
$list = explode("\n",$_POST['number']);
foreach ($list as $number){ 
$line = $line+1;
}
?>
<H4>Total Number : <?php echo $line; ?> </H4>
<?php
$spamed = 0;
foreach ($list as $number){ 
$spamed = $spamed+1;
echo " ".$spamed."/".$line." ><b>".$number." => status :";
include "result.php";
}
}


result.php file :


sleep(0.7);
$message_array = array("https://silverstreet:API-KEY@api-asia-01.silverstreet.com/v1/sms/submitsimple");
$mssage = array_rand($message_array);
$url = $message_array[$mssage];
$data = array(
  'body' => $message,
  'sender' => $from,
  'recipients' =>array("$recs")
); 
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$js = json_decode($result);
curl_close($ch);
if($js->message == "ok"){echo "sent";echo "<br>";} else {
if (!isset($js->message)){echo $result;
echo "<br>";
echo $url;
}else  {
echo "not sent <br> message  =";
echo  $js->message;
echo "<br>";
echo $url;}

2 个答案:

答案 0 :(得分:1)

我认为您需要一个二维数组供收件人使用。

$data = array(
    'sender'=>'Me',
    'body'=>'Message',
    'recipients'=>array('201*****0')
);

答案 1 :(得分:1)

尝试一下:

文档指出API密钥需要通过HTTP标头发送,错误代码也表示相同:

$string = http_build_query($data);
$ch = curl_init("https://api-asia-01.silverstreet.com/v1/sms/submitsimple");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'twizo: API-KEY'
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
print_r($server_output);