Send SMS by PHP

Asked

Viewed 591 times

0

I have a SMS gateway that receives 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, by means of a 'SELECT' from my database, print in an input field and send the form. However, the operator does not allow to send more than 15 simultaneous messages. It would be possible to send one by one, but giving only a SEND in the form?

  • 1

    A loop loop wouldn’t solve the problem?

  • The first thing that came to mind was what @Marcelodeandrade said, a loop, for, foreach, while, the $Numbers vc array already has.

  • Try using the method sleep(). It causes the PHP script to be delayed by the set time and then run... Another thing you can do is use a <meta refresh>

2 answers

1

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

1) In your bank’s select, create a field: status_envio with the value 0 by default.

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

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

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

5) Place at the end of the script execution loop, the printing of 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 must 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; 
}
  • This code I pasted, refers to how the gateway, I have no access, since it is outsourced, accepts the data. I made in the send form, the post goes to this gateway.

  • 1

    All right, Leandro, I made an example, you don’t need to create the method inside the gateway, do it on your system. You only need access to the status field.

  • got it, I’ll test it here. Thank you ^^

0

Using a repeat loop 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 Smss, you can add a sleep.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.