Implementing SMPP(Short Message Peer-to-Peer) integration using Laravel involves setting up a Laravel application to communicate with SMPP servers for sending and receiving SMS messages.
Here's a basic guide to get started:
Initially, we require two composers.
composer require franzose/laravel-smpp
composer require php-smpp/php-smpp
Provide the necessary configuration parameters such as the SMPP server IP, port, user name, phone, header, message, emplate id and entity id, etc either directly in the service class or preferably via Laravel's configuration files like .env for better flexibility.
Routes :
Route::get('/send', [HomeController::class, 'send'])->name('home.send');
Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use LaravelSmpp\LaravelSmppServiceProvider;
use LaravelSmpp\SmppServiceInterface;
use Exception;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Arr;
use Log;
use SMPP;
use SmppAddress;
use SmppClient;
use SmppException;
use SocketTransport;
use GsmEncoder;
use SmppTag;
use SmppDeliveryReceipt;
class HomeController extends Controller
{
public function __construct(){
}
public function send(SmppServiceInterface $smpp){
$server='{IP ADDERESS}';
$port='{PORT}';
$username='{USERNAME}';
$password='{PASSWORD}';
$phone='{NUMBER}';
$header='{HEADER}';
$message='{MESSAGE}';
// Construct transport and client
$transport = new SocketTransport(array($server), $port);
$transport->setRecvTimeout(30000); // for this example wait up to 30 seconds for data
$transport::$forceIpv4 = true;
$transport->setSendTimeout(30000); // for this example wait up to 30 seconds for data
$smpp = new SmppClient($transport);
// Activate binary hex-output of server interaction
$smpp->debug = true;
$transport->debug = true;
// Open the connection
$transport->open();
$smpp->bindTransmitter($username, $password);
// Prepare message
$encodedMessage = GsmEncoder::utf8_to_gsm0338($message);
$from = new SmppAddress($header, SMPP::TON_ALPHANUMERIC, SMPP::NPI_UNKNOWN);
$to = new SmppAddress($phone, SMPP::TON_INTERNATIONAL, SMPP::NPI_E164);
$tags = array(
new SmppTag(0x1400, '{ENTITY_ID}'),
new SmppTag(0x1401, '{TEMPLATE_ID}')
);
$from->ton = 0;
$from->npi = 0;
$to->ton = 0;
$to->npi = 0;
$message_id = $smpp->sendSMS($from, $to, $encodedMessage, $tags);
$result = $this->readSms();
echo '<pre>';print_r($result);die;
}
function readSms(){
$transport = new SocketTransport(array('{IP ADDERESS}'),{PORT});
$transport->setRecvTimeout(30000); // for this example wait up to 30 seconds for data
$smpp = new SmppClient($transport);
// Activate binary hex-output of server interaction
$smpp->debug = true;
$transport->debug = true;
// Open the connection
$transport->open();
$smpp->bindReceiver('{USERNAME}','{PASSWORD}');
// Read SMS and output
$sms = $smpp->readSMS();
return $sms;
}
}
By implementing SMPP integration using Laravel, you can leverage the robust features of Laravel framework while harnessing the power of SMPP for reliable SMS communication. This integration can be particularly useful for applications requiring SMS notifications, two-factor authentication, or other SMS-based functionalities. With careful planning and implementation, you can enhance the communication capabilities of your Laravel application and provide a seamless experience for your users.
Happy Coding 😐