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\SQLDatabase;
use TimeFrontiers\Sms\Dto\WebhookRequest;
use TimeFrontiers\Sms\Sms;

/** @var SQLDatabase $database */
Sms::configure([
    'db_name' => 'messaging',
    'default_driver' => 'twilio',
    'default_sender' => 'MyApp',
    'region_strategy' => 'auto',
    'continent_mapping' => [
        'Africa' => 'africastalking',
    ],
    'idempotency_scope' => 'production',
    'max_segments' => 5,
    'stale_dispatch_seconds' => 300,
    'default_region' => 'NG',
    'content_retention_days' => 30,
    'logger' => static function (string $code, Throwable $error, array $context): void {
        // Send only to a trusted logger that redacts credentials and PII.
    },
    'drivers' => [
        'twilio' => [
            'sid' => getenv('TWILIO_ACCOUNT_SID'),
            'token' => getenv('TWILIO_AUTH_TOKEN'),
            'default_sender' => '+15005550006',
            'allowed_senders' => ['+15005550006', 'MyApp'],
            'webhook_url' => 'https://api.example.com/webhooks/sms/twilio',
        ],
        'africastalking' => [
            'app_id' => getenv('AT_USERNAME'),
            'api_key' => getenv('AT_API_KEY'),
            'default_sender' => 'MyApp',
            'allowed_senders' => ['MyApp'],
            'webhook_verifier' => static function (WebhookRequest $request): bool {
                // Authenticate a shared gateway secret, mTLS identity, or an
                // equivalent application-controlled trust boundary.
                return hash_equals(
                    (string) getenv('SMS_GATEWAY_SECRET'),
                    (string) $request->header('x-sms-gateway-secret'),
                );
            },
        ],
    ],
], $database);

use TimeFrontiers\Sms\Result\SendResult;
use TimeFrontiers\Sms\Sms;

$result = Sms::sendResult([
    'receiver' => '+234 802 429 6777',
    'message' => 'Your verification code is 123456',
    'idempotency_key' => 'otp:user-828000000000001:challenge-42',
    'idempotency_scope' => 'auth',
]);

switch ($result->outcome) {
    case SendResult::ACCEPTED:
    case SendResult::REPLAYED:
        $sms = $result->sms;
        break;

    case SendResult::IN_FLIGHT:
        // An identical request owns the dispatch claim. Do not send again.
        $sms = $result->sms;
        break;

    case SendResult::UNKNOWN:
        // The provider may have accepted it. Do not send again. Reconcile it.
        $sms = $result->sms;
        break;

    default:
        $safeErrors = $result->errors;
}

$sms = Sms::send($data, $driver);       // Sms|false
$sms = Sms::sendAndWait($data, $driver); // Sms|false

$sms = Sms::send($data);
if ($sms === false) {
    $errors = Sms::lastErrors();
} else {
    // Optional consumer dependency:
    $errors = (new \TimeFrontiers\InstanceError($sms, $accessRank))->get();
}

$sms->feesExact(); // "0.12500000"
$sms->fees();      // compatibility-only float view

$request = new WebhookRequest(
    rawBody: file_get_contents('php://input'),
    headers: getallheaders(),
    method: $_SERVER['REQUEST_METHOD'],
    // For Twilio JSON callbacks, : $_POST,
);

$result = Sms::deliveryReportResult('twilio', $request);

$sms = Sms::findByCode('828123456789012');
$sms = Sms::findByProviderReference('twilio', 'SM123');
$sms = Sms::findByReference('SM123', 'twilio');

final class MyDriver implements \TimeFrontiers\Sms\Driver\SmsDriverInterface
{
    public function send(Sms $sms): ProviderSendResult { /* ... */ }
    public function verifyDeliveryReport(WebhookRequest $request): bool { /* ... */ }
    public function parseDeliveryReport(WebhookRequest $request): ParsedDeliveryReport { /* ... */ }
    public function getProviderName(): string { return 'mydriver'; }
}
bash
composer 
bash
php bin/sms-backfill-codes --bootstrap=/absolute/path/to/bootstrap.php
php bin/sms-backfill-codes --bootstrap=/absolute/path/to/bootstrap.php --apply
bash
php bin/sms-recover --bootstrap=/absolute/path/to/bootstrap.php
php bin/sms-recover --bootstrap=/absolute/path/to/bootstrap.php --apply
bash
php bin/sms-purge --bootstrap=/absolute/path/to/bootstrap.php
php bin/sms-purge --bootstrap=/absolute/path/to/bootstrap.php --apply