首页 > 解决方案 > 如何修复此数组并使循环仅用于 textarea 数字?

问题描述

此代码适用于 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']
    )
);

我尝试了很多事情,但它总是给我错误或只发送到第一个号码或将它们放在一起,但我不需要它在一秒钟内一起发送给他们我需要它发送到一个然后中断然后发送到下一个文本区域等

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

<?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 表单以一个方式发送到 textarea 中的所有数字,然后发送到第一行中的第一个数字,然后通过添加此 BREAK 发送到第二行

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


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

标签: phparraysloops

解决方案


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

其次,您使用 Twilio 消息传递系统错误。您可以在文档中查看如何发送多条消息。

尝试将您的代码修改为:

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

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


推荐阅读