PHP code example of timefrontiers / php-sms

1. Go to this page and download the library: Download timefrontiers/php-sms library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

timefrontiers / php-sms example snippets


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
    ],
]);

$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();
}

$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'
]);

// 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();

$updated = Sms::processDeliveryReport('twilio', $_POST);
if ($updated) {
    // $updated->status() is now 'delivered' or 'failed'
}

$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}"),
];

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

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'; }
}

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

$ie = new \TimeFrontiers\InstanceError($sms, $session->access_rank);
echo $ie->first();
bash
composer