定制 timefrontiers/php-sms 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

timefrontiers/php-sms

Composer 安装命令:

composer require timefrontiers/php-sms

包简介

Multi-driver SMS messaging for PHP 8.4+. Handles outbound sends, delivery webhooks, message logging, and driver‑based routing with automatic continent selection.

README 文档

README

Multi‑driver SMS messaging for PHP 8.4+. Handles outbound sends, delivery webhooks, message logging, and driver‑based routing with automatic continent selection.

Requirements

  • PHP 8.4+
  • MariaDB 10.4+ / MySQL 8.0+
  • timefrontiers/php-core — phone number country / continent detection
  • timefrontiers/php-data — random code generation
  • timefrontiers/php-sql-database
  • timefrontiers/php-database-object
  • timefrontiers/php-pagination
  • timefrontiers/php-instance-error
  • timefrontiers/php-validator
  • twilio/sdk — required when using the Twilio driver
  • africastalking/africastalking — required when using the AfricasTalking driver

Installation

composer require timefrontiers/php-sms

Database

Run sql/install.sql against your messaging database (e.g. messaging) to create the sms table.

Migrating from linktude/php-sms? See sql/migrate.sql.

Configuration

Call Sms::configure() once in your application bootstrap, before any SMS message is sent.

use TimeFrontiers\Sms\Sms;

Sms::configure([
    'db_name'         => 'messaging',     // database containing the `sms` table
    'default_driver'  => 'twilio',        // fallback driver when no explicit driver is given
    'default_sender'  => 'MyApp',         // default sender ID / phone number
    'region_strategy' => 'auto',          // 'auto' = use continent mapping; or a fixed driver name
    'continent_mapping' => [            // maps continent → driver (used when strategy = 'auto')
        'Africa' => 'africastalking',
    ],
    'drivers' => [
        'twilio' => [
            'sid'          => 'AC...',
            'token'        => 'your-auth-token',
            'sender_id'    => 'MyApp',           // alphanumeric sender ID (if supported)
            'sender_phone' => '+1234567890',     // fallback long code
        ],
        'africastalking' => [
            'app_id'  => '...',
            'api_key' => '...',
            'sender_id' => 'MyApp',
        ],
        // future drivers: just add a key and credentials
    ],
]);

You can override any driver setting at send time.

Usage

Sending an SMS

$sms = Sms::send([
    'receiver' => '+2348024296777',
    'message'  => 'Your verification code is 123456',
]);

if ($sms) {
    echo "Message sent! Code: " . $sms->code();
} else {
    $errors = \TimeFrontiers\InstanceError($sms, true);
    echo "Error: " . $errors->first();
}

send() returns a Sms instance (with status = 'sent') on success, or false on failure. Errors are accessible via the HasErrors trait (_userError, _systemError). Read them with InstanceError (rank‑filtered).

You may also call sendAndWait() — an alias that keeps API symmetry for future async support.

Override defaults per message

$sms = Sms::send([
    'receiver' => '+254700000000',
    'message'  => 'Jambo!',
    'driver'   => 'africastalking',  // explicit driver
    'sender'   => 'MyBrand',         // override sender
    'user'     => $userCode,         // associate with a user (default 'SYSTEM')
    'batch'    => 'BATCH-001',       // group multiple messages
    'message_id' => 42,              // reply‑to an existing message
    'direction' => 'outbound',       // 'outbound' (default) or 'inbound'
]);

If driver is not supplied, the package resolves the driver according to region_strategy:

  • 'auto' — determines the phone number’s continent via timefrontiers/php-core and picks the driver from continent_mapping, falling back to default_driver.
  • Any string — uses that driver directly.

Message lookups

// By unique code
$sms = Sms::findByCode('8280000000001');

// By provider reference (message SID / messageId)
$sms = Sms::findByReference('SM1234567890');

// Paginated messages for a user
$messages = Sms::query()
    ->where('user', $userCode)
    ->orderByDesc('_created')
    ->limit(20)
    ->get();

Delivery reports (webhooks)

Handle incoming status callbacks from Twilio or AfricasTalking with a single static call:

$updated = Sms::processDeliveryReport('twilio', $_POST);
if ($updated) {
    // $updated->status() is now 'delivered' or 'failed'
}
  • The driver verifies the webhook signature (verifyDeliveryReport).
  • The payload is parsed into a normalized reference and status (parseDeliveryReport).
  • The corresponding sms row is loaded and its status updated.
  • Driver‑specific metadata is merged into the meta JSON column.
  • Returns the updated Sms instance or null if verification fails.

If you need a fully custom delivery endpoint, you can instantiate a driver directly and call its verifyDeliveryReport / parseDeliveryReport methods.

Message entity

The Sms class represents a single row in the sms table and uses DatabaseObject + Pagination + HasErrors.

Public getters

Method Returns Description
id() ?int Auto‑increment primary key
code() ?string 828-prefixed 15‑char unique code
status() string pending, queued, sent, failed, delivered
messageId() ?int Parent message ID (for reply chains)
direction() string outbound or inbound
user() string Owner code (default 'SYSTEM')
batch() ?string Batch group code
sender() string Sender ID / phone used
receiver() string Recipient phone number
message() string Message body (max 250 chars)
messagePages() int Calculated message parts (1‑5)
fees() float Cost incurred
feesCurrency() ?string Currency of the fee
reference() ?string Provider‑assigned ID
meta() ?array Driver‑specific metadata (decoded JSON)

Pagination

The Pagination trait is available:

$sms = new Sms($conn);
$sms->setPage($page)->setPerPage(20);
$conn   = $sms->conn();
$total  = $conn->fetchOne("SELECT COUNT(*) AS c FROM `messaging`.`sms` WHERE `user` = ?", [$userCode]);
$sms->setTotalCount((int)($total['c'] ?? 0));

$rows   = $conn->fetchAll(
    "SELECT * FROM `messaging`.`sms`
      WHERE `user` = ?
      ORDER BY `_created` DESC " . $sms->limitClause(),
    [$userCode]
);
return [
    'items' => $rows,
    'meta'  => $sms->paginationMeta("https://api.example.com/sms?user={$userCode}"),
];

Query Builder

$pending = Sms::query()
    ->where('status', 'pending')
    ->where('direction', 'outbound')
    ->orderBy('_created')
    ->limit(50)
    ->get();

Custom drivers

Implement TimeFrontiers\Sms\Driver\SmsDriverInterface:

class MyDriver implements SmsDriverInterface
{
    public function send(Sms $sms): array
    {
        // return [cost, currency, reference, senderUsed]
    }
    public function verifyDeliveryReport(array $payload): bool { ... }
    public function parseDeliveryReport(array $payload): array
    {
        // return ['reference' => ..., 'status' => 'delivered'|'failed', 'meta' => [...]]
    }
    public function getProviderName(): string { return 'mydriver'; }
}

Register it in configuration:

Sms::configure([
    ...
    'drivers' => [
        'mydriver' => [ 'api_key' => '...' ],
    ],
]);

Then pass 'driver' => 'mydriver' when sending.

Error handling

  • The Sms class uses the HasErrors trait. Validation failures and driver errors are recorded as _userError (rank 0) or _systemError (rank 7).
  • To read errors filtered by access rank, wrap the instance in InstanceError:
$ie = new \TimeFrontiers\InstanceError($sms, $session->access_rank);
echo $ie->first();
  • Exceptions are thrown only for truly exceptional conditions (missing configuration, unknown driver). Normal send failures return false and populate errors.

Database migration

If you are upgrading from linktude/php-sms:

  1. Run sql/migrate.sql to update the schema.
  2. Then call Sms::populateMissingCodes($conn) to fill the new code column with unique 828‑prefixed identifiers.

License

MIT

timefrontiers/php-sms 适用场景与选型建议

timefrontiers/php-sms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 timefrontiers/php-sms 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2026-04-27