如何修复此数组并使循环仅用于文本区域编号?

时间:2019-10-06 12:22:28

标签: php arrays loops

此代码是Twilio SMS的正常代码,我在这里尝试将其发送给未知数量的数字,例如20个数字,因此我无法识别该数组 我需要代码仅在$ number上起作用,因为数组发送到TextArea的每个数字并中断,然后发送到下一个数字,直到发送到所有数字 但是发送给前2个号码是行不通的,只有任何人都可以帮忙,代码有什么问题?

<form action="bulk.php" method="post">
<p> From: </p> <input type="text" name="sender" autocomplete="on" /><br>
<p> To: </p><textarea rows="10" cols="50" name="textareaname"></textarea><br>
<p> Message </p>
<textarea name="message" maxlenght="19304" rows="4" ></textarea><br>
<br><input type="submit" value="Send SMS" />
</form>

<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'mysid';
$token = 'mytoken' ;
$client = new Client($sid, $token);
// This part is the array that i added to code the problem here
$text = trim($_POST['textareaname']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim');

// foreach ($textAr as $key) {

foreach ($textAr as $key => $value) {
  $numbers =   $value.PHP_EOL;
}
// End of my bulk sending  Array code 

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    // Here i tried to put the loop array instead one number
   $numbers,
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => $_POST['sender'],
        // the body of the text message you'd like to send
        'body' => $_POST['message']
    )
);

我尝试了很多事情,但是它总是给我错误或仅发送给第一个数字o将它们放在一起,但是我不需要一秒钟将它们一起发送给我,我需要将其发送给一个然后断开然后发送到textarea等的下一个

这是仅发送给单个号码的原始代码

<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'mysid';
$token = 'mytoken';
$client = new Client($sid, $token);

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    $_POST['number'],
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => $_POST['sender'],
        // the body of the text message you'd like to send
        'body' => $_POST['message']
    )
);

我试图将textarea形式与Array一起使用,以一一发送到textarea中的所有数字 表示发送到第一行的第一个号码,然后BREAK发送到第二行 通过添加此

$text = trim($_POST['textareaname']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim');


foreach ($textAr as $key => $value) {
  $numbers =   $value.PHP_EOL;
}

1 个答案:

答案 0 :(得分:1)

您的代码有2个问题。首先,您使用字符串覆盖$numbers var。您应该将[]运算符用作$numbers[] = $value.PHP_EOL;

第二,您使用Twilio消息传递系统错误。您可以在documentation中查看如何发送多封邮件。

尝试将您的代码修改为:

foreach ($textAr as $value) {
    $client->messages->create(
        $value,
        array(
            'from' => $_POST['sender'],
            'body' => $_POST['message']
        )
    );
}

这样,您可以在每次迭代中进行发送,并避免创建覆盖的数组。