Reputation: 39
This code is for Twilio SMS the normal i'm trying here to make it send to unknown amount of numbers for example 20 numbers so i can't identify the array I need the code works on the $number only as array send to each number of the TextArea and break and then send to next number until send to All numbers But it didn't work it sent to first 2 numbers only can anyone help , what is the wrong in the code?
<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']
)
);
I tried many things but it always gives me error or send to first number only o put them together , but i don't need it send to them together in one second i need it send to one and then break and then send to next one of textarea , etc
This is the original code that sends to single number only
<?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']
)
);
i tried to use textarea form with Array to send to all numbers in textarea one by one means send to first number in first line and BREAK then send to second line by add this
$text = trim($_POST['textareaname']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim');
foreach ($textAr as $key => $value) {
$numbers = $value.PHP_EOL;
}
Upvotes: 0
Views: 154
Reputation: 11642
Your code has 2 issue. First you override the $numbers
var with string. you should use the []
operator as $numbers[] = $value.PHP_EOL;
.
Second, you using Twilio messaging system wrong. You can see in the documentation how to send multi-messages.
Try modify your code to:
foreach ($textAr as $value) {
$client->messages->create(
$value,
array(
'from' => $_POST['sender'],
'body' => $_POST['message']
)
);
}
This way you do the sending in each iteration and avoiding create the array you overwrite.
Upvotes: 1