Barry Conway
Barry Conway

Reputation: 1

php-smpp send multiple SMS

I am using the library https://github.com/alexandr-mironov/php-smpp which I have implemented and works successfully, but I can only send one SMS at a time. I have several messages that need to be sent, has anyone used this library and been able to batch or loop multiple sms. If so sharing your code / thoughts would be much appreciated.

use smpp\{ Address, SMPP, Client as SmppClient, transport\Socket};

class SmsBuilder
{
const DEFAULT_SENDER = 'github_example';
protected $transport;
protected $smppClient;
protected $debug = false;
protected $from;
protected $to;
protected $login;
protected $password;

/**
 * SmsBuilder constructor.
 * @param string $address SMSC IP
 * @param int $port SMSC port
 * @param string $login
 * @param string $password
 * @param int $timeout timeout of reading PDU in milliseconds
 * @param bool $debug - debug flag when true output additional info
 */
public function __construct(
    string $address,
    int $port,
    string $login,
    string $password,
    int $timeout = 10000,
    bool $debug = false
)
{
    $this->transport = new Socket([$address], $port);
    $this->transport->setRecvTimeout($timeout);
    $this->smppClient = new SmppClient($this->transport);

    // Activate binary hex-output of server interaction
    $this->smppClient->debug = $debug;
    $this->transport->debug = $debug;

    $this->login = $login;
    $this->password = $password;

    $this->from = new Address(self::DEFAULT_SENDER,SMPP::TON_ALPHANUMERIC);
}

/**
 * @param $sender
 * @param $ton
 * @return $this
 * @throws \Exception
 */
public function setSender($sender, $ton)
{
    return $this->setAddress($sender, 'from', $ton);
}

/**
 * @param $address
 * @param $ton
 * @return $this
 * @throws \Exception
 */
public function setRecipient($address, $ton)
{
    return $this->setAddress($address, 'to', $ton);
}

/**
 * @param $address
 * @param string $type
 * @param int $ton
 * @param int $npi
 * @return $this
 * @throws \Exception
 */
protected function setAddress($address, string $type, $ton = SMPP::TON_UNKNOWN, $npi = SMPP::NPI_UNKNOWN)
{
    // some example of data preparation
    if($ton === SMPP::TON_INTERNATIONAL){
         $npi = SMPP::NPI_E164;
    }
    $this->$type = new Address($address, $ton, $npi);
    return $this;
}

/**
 * @param string $message
 */
public function sendMessage(string $message)
{
    $this->transport->open();
    $this->smppClient->bindTransceiver($this->login,$this->password);
    // strongly recommend use SMPP::DATA_CODING_UCS2 as default encoding in project to prevent problems with non latin symbols
    $this->smppClient->sendSMS($this->from, $this->to, $message, null, SMPP::DATA_CODING_UCS2);
    $this->smppClient->close();
 }
}

/**
 *  send two sms's
 */

$data= new stdClass(); ['send','cell','message'];
$data->send='Nendersno';
$data->cell='RecipientNo';
$data->message='My message';
$arr=array();
array_push( $arr,$data);
array_push( $arr,$data);

$sms=new SmsBuilder('192.168.1.1', '2776', 'your_login', 'your_password', 10000));
foreach ($arr as $k => $v) {
  $sms->setSender($v->send, \smpp\SMPP::TON_ALPHANUMERIC);
  $sms->setRecipient($v->cell,\smpp\SMPP::TON_INTERNATIONAL);
  $sms->sendMessage($v->message); 
 }

Upvotes: 0

Views: 957

Answers (1)

Mohammed Huzaif
Mohammed Huzaif

Reputation: 31

Why not try Plivo to send bulk SMS? You can try this documentation to build the same thing in a much easier way.

Upvotes: 0

Related Questions