kstmostofa/laravel-smpp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kstmostofa/laravel-smpp

Composer 安装命令:

composer require kstmostofa/laravel-smpp

包简介

A Laravel package for sending SMS using the SMPP protocol, built on top of the PHP-SMPP library. This package provides a simple and efficient way to integrate SMS functionality into your Laravel applications.

README 文档

README

A modern Laravel-friendly wrapper around an SMPP v3.4 client for sending and receiving SMS, with delivery receipt (DLR) support, connection management, and extensible listener patterns.

Features

  • Send single or concatenated (long) SMS (GSM 03.38 / UCS-2)
  • Request and parse Delivery Receipts (DLRs)
  • Receive MO (mobile originated) messages
  • Transmitter / Receiver / Transceiver bind modes
  • Auto DNS resolution, IPv4 / IPv6 (with force flags)
  • Custom timeouts & non-blocking connect with fallback
  • Clean Laravel Facade API
  • You create your own long‑running DLR listener (example provided)

Installation

composer require kstmostofa/laravel-smpp

Auto-discovery registers the service provider and facade.

Configuration

Publish config:

php artisan vendor:publish --provider="Kstmostofa\\LaravelSmpp\\LaravelSmppServiceProvider" --tag="smpp-config"

config/smpp.php:

return [
  'host' => env('SMPP_HOST','127.0.0.1'),
  'port' => env('SMPP_PORT',2775),
  'username' => env('SMPP_USERNAME','smppuser'),
  'password' => env('SMPP_PASSWORD','smpppass'),
  'timeout' => env('SMPP_TIMEOUT',10000),
  'debug' => env('SMPP_DEBUG',false),
];

Quick Send Example

use Kstmostofa\LaravelSmpp\Facades\LaravelSmpp;
use Kstmostofa\LaravelSmpp\SMPP;

LaravelSmpp::getTransport()->open();
LaravelSmpp::bindTransceiver();
$id = LaravelSmpp::setSender('SENDER', SMPP::TON_ALPHANUMERIC)
    ->setRecipient('1234567890', SMPP::TON_INTERNATIONAL)
    ->requestDLR()
    ->sendSMS('Hello world');
LaravelSmpp::close();

Creating Your Own DLR / MO Listener

DLRs and MO messages are asynchronous. Implement a custom Artisan command that keeps a persistent bind and loops reading PDUs.

1. Make a Command

php artisan make:command SmppReceive --command=smpp:receive

2. Implement Logic (app/Console/Commands/SmppReceive.php)

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Kstmostofa\LaravelSmpp\Facades\LaravelSmpp;
use Kstmostofa\LaravelSmpp\DeliveryReceipt;
use Kstmostofa\LaravelSmpp\Sms;
use Kstmostofa\LaravelSmpp\Transport\Socket;

class SmppReceive extends Command
{
    protected $signature = 'smpp:receive {--host=} {--port=} {--username=} {--password=} {--timeout=} {--debug}';
    protected $description = 'Listen for SMPP Delivery Receipts (DLRs) and MO SMS messages';

    public function handle()
    {
        $cfg = config('smpp');
        foreach(['host','port','username','password','timeout'] as $k){ if($this->option($k)!==null) $cfg[$k]=$this->option($k); }
        if($this->option('debug')) $cfg['debug']=true;

        // Optional: force IPv4 in problematic networks
        // Socket::$forceIpv4 = true;

        try {
            LaravelSmpp::setConfig($cfg);
            LaravelSmpp::getTransport()->open();
            LaravelSmpp::bindTransceiver();
            $this->info('Connected & bound: '.json_encode($cfg));
        } catch(\Throwable $e){
            $this->error('Initial connect failed: '.$e->getMessage());
            return 1;
        }

        while(true){
            try {
                $pdu = LaravelSmpp::readSMS();
                if($pdu instanceof DeliveryReceipt){
                    // Persist/update message status in DB
                    $this->info('[DLR] id='.$pdu->messageId.' status='.$pdu->status);
                } elseif($pdu instanceof Sms){
                    // Store inbound MO
                    $this->info('[MO ] from='.$pdu->source->value.' text='.$pdu->message);
                }
                // Keep link alive (adjust cadence as needed)
                LaravelSmpp::enquireLink();
                usleep(100000); // 100ms
            } catch(\Throwable $e){
                $this->error('Loop error: '.$e->getMessage());
                sleep(1);
                try { LaravelSmpp::reconnect(); } catch(\Throwable $re){ $this->error('Reconnect failed: '.$re->getMessage()); }
            }
        }
    }
}

3. Run Listener

php artisan smpp:receive --host=smpp.example.com --username=user --password=pass --debug

Manage via Supervisor/systemd for production.

Runtime Config Override

LaravelSmpp::setConfig([
  'host'=>'alt.host','port'=>2776,'username'=>'alt','password'=>'secret','timeout'=>15000,'debug'=>true,
]);

Forcing IPv4 / IPv6

use Kstmostofa\LaravelSmpp\Transport\Socket;
Socket::$forceIpv4 = true; // or Socket::$forceIpv6 = true;

Debugging

Set SMPP_DEBUG=true or pass --debug. Debug output uses error_log.

Troubleshooting

Issue Tip
Could not connect / Operation now in progress Check firewall, force IPv4, correct host/port
Bind Failed Verify credentials, system type permissions
No DLR Ensure requestDLR() chained & listener running
Truncated >160 chars Library segments automatically if GSM/UCS2; verify data_coding
Stuck/slow Increase timeout, verify network latency

Contributing

PRs welcome (tests + README update).

License

MIT

kstmostofa/laravel-smpp 适用场景与选型建议

kstmostofa/laravel-smpp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 623 次下载、GitHub Stars 达 12, 最近一次更新时间为 2025 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「sms」 「gsm」 「smpp」 「texting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kstmostofa/laravel-smpp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kstmostofa/laravel-smpp 我们能提供哪些服务?
定制开发 / 二次开发

基于 kstmostofa/laravel-smpp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 623
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-14