Twizo SMS:此字段仅允许使用数组值

时间:2019-06-10 16:28:29

标签: php json curl sms

更新1:我无法使其正常工作,请参见下面的更新代码。 API说明:https://www.twizo.com/developers/documentation/

使用下面的代码发送SMS时,返回错误:

  

{“ validation_messages”:{“ recipients”:{“ noArraySupplied”:“此字段的唯一数组>值”}},“ type”:“ http://www.w3.org/Protocols/rfc2616/rfc2616- ....... ..> sec10.html“,” title“:”不可处理的> Entity“,” status“:422,” detail“:”验证失败“}

send.php文件:

<head>
  <meta charset="utf-8">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>  input[type=submit] { width: 50%; background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } 
input[type=submit]:hover { background-color: #45a049; } 
div { border-radius: 5px; background-color: #f2f2f2; padding: 20px; } </style>
<div>
<center>
<form method="post" action="">
Subject
<input type="text" name="from" value=<?php if(isset($_POST['from'])){echo $_POST['from']; } ?> ><br>
<textarea class="form-control" name="message" rows="5" placeholder="Put Message Here "><?php if(isset($_POST['message'])){echo $_POST['message']; } ?>
</textarea><BR/><BR/>

<textarea class="form-control" name="number"
 rows="5" placeholder="Put Number List Here"><?php if(isset($_POST['number'])){echo $_POST['number']; } ?>
</textarea><BR/><BR/>


<input type="submit" name="submit" value="Send"/>


</form>



<?php 
set_time_limit(0);
if (isset($_POST['submit'])) {
    $letter = $_POST['message'];
    $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";
    }
}


?>

</center>

result.php文件:

sleep(0.7);
$message_array = array("https://twizo:API-KEY@api-eu-01.twizo.com/v1/sms/submitsimple");
$mssage = array_rand($message_array);
$url = $message_array[$mssage];
$data = array('recipients' => ("".$number), 'body' => $message, 'sender' => $from);
$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;
    }
}

1 个答案:

答案 0 :(得分:1)

result.php

您的帖子字段(收件人)必须是一个数组。

您当前的行

$data = array('recipients' => ("".$number), 'body' => $message, 'sender' => $from);

更改为

$data = array('recipients[]' => ($number), 'body' => $message, 'sender' => $from);

您只需要添加[]即可将该字段转换为数组。