Send SMS via PHP

0

I have an SMS gateway that receives the data from my form in this pattern:

<?php
include "smsGateway.php";
$smsGateway = new SmsGateway('[email protected]', 'password');

$deviceID = 1;
$numbers = ['+44771232343', '+44771232344'];
$message = 'Hello World!';

$options = [
'send_at' => strtotime('+10 minutes'), // Send the message in 10 minutes
'expires_at' => strtotime('+1 hour') // Cancel the message in 1 hour if the     message is not yet sent
];

//Please note options is no required and can be left out
$result = $smsGateway->sendMessageToManyNumbers($number, $message, $deviceID, $options);
?>

I send the numbers, through a SELECT * from my database, I give print in an input field and send the form. However, the carrier does not allow more than 15 simultaneous messages to be sent. Would it be possible to send one by one but only give a SEND on the form?

    
asked by anonymous 18.07.2016 / 16:22

2 answers

1

One way to do this is by creating a loop, and a check by sending status:

1) At the select of your bank, create a field: status_envio with value 0 as default.

2) Then make a select with the condition: WHERE statust_envio=0 LIMIT 15 .

3) For each occurrence of 15 submissions, update the status of these records to 1 .

4) When no more records are found, please report the end of the process.

5) Put at the end of the script execution loop, printing a metatag or a javascript with a timeOut.

$timeRefresh = '15'; //15 segundos

$url = $_SERVER['SCRIPT_NAME'];

$result = array();
if (count($numbers)) {
   foreach ($numbers as $key => $number) {
       $result[$key] = $smsGateway->sendMessageToManyNumbers($number, $message, $deviceID, $options);
       if ($result[$key]) {
          $update = $smsGateway->updateStatus($number, 1);
       }
   }
  echo "<meta http-equiv=refresh content=\"{$timeRefresh};URL={$url}\">";
} else {
//e reatualize todo o banco novamente com status = 0 se precisar que esse processo seja repetido toda vez que chegar ao final e precisar enviar a página
  $update = $smsGateway->updateStatus(null, 0);
  echo "Fim dos envios!";

}

OBS: Check method should update status:

public function updateStatus($number = null, $status = 0)
{
   $executed = null;

   if ( $number != null ) {
       $SQL = 'UPDATE tabela_sms set statust_envio=:status where number=:number';
       $stmt = $this->db->prepare($SQL);
       $executed = $stmt->execute(['status' => $status, 'number'=>$number]);
   } else {
      //se for nulo, atualiza o status para todos
       $SQL = 'UPDATE tabela_sms set statust_envio=:status where 1';
       $stmt = $this->db->prepare($SQL);
       $executed = $stmt->execute(['status' => $status]);
   }
   return $executed; 
}
    
18.07.2016 / 20:01
0

Using a loopback you would solve this problem as follows:

foreach ($numbers as $number) {
    $result[] = $smsGateway->sendMessageToManyNumbers($number, $message, $deviceID, $options);
}

If there is a time limit for sending SMS, you can add a sleep .

    
18.07.2016 / 19:07