PHP code example of itxshakil / laravel-fast2sms

1. Go to this page and download the library: Download itxshakil/laravel-fast2sms 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/ */

    

itxshakil / laravel-fast2sms example snippets


use Shakil\Fast2sms\Facades\Fast2sms;

// Send a quick SMS
$response = Fast2sms::quick(
    numbers: '9876543210',
    message: 'Your OTP is 123456',
);

if ($response->isSuccess()) {
    echo 'SMS sent! Request ID: ' . $response->requestId;
}

use Shakil\Fast2sms\Notifications\Messages\SmsMessage;

$msg = new SmsMessage('Your OTP is 123456');

$msg->charCount();      // 22
$msg->isUnicode();      // false
$msg->creditCount();    // 1
$msg->exceedsOneSms();  // false

use Shakil\Fast2sms\Facades\Fast2sms;

Fast2sms::quick(numbers: '9876543210', message: 'Hello from Fast2SMS!');

Fast2sms::otp(
    numbers: '9876543210',
    otpValue: '123456',
);

Fast2sms::dlt(
    numbers: ['9876543210', '9123456789'],
    templateId: 'your_template_id',
    variablesValues: 'Your order #1234 has been shipped.',
    senderId: 'MYSHOP',
);

Fast2sms::to('9876543210')
    ->message('Flash message!')
    ->flash()
    ->send();

Fast2sms::quick(
    numbers: ['9876543210', '9123456789', '9000000001'],
    message: 'Broadcast message',
);

Fast2sms::whatsapp()
    ->to('919876543210')
    ->sendText('Hello from Fast2SMS!');

Fast2sms::whatsapp()
    ->to('919876543210')
    ->sendImage('https://example.com/image.jpg');

Fast2sms::whatsapp()
    ->to('919876543210')
    ->sendDocument('https://example.com/invoice.pdf');

Fast2sms::whatsapp()
    ->to('919876543210')
    ->sendLocation(latitude: 28.6139, longitude: 77.2090, name: 'New Delhi');

Fast2sms::whatsapp()
    ->to('919876543210')
    ->sendInteractive([
        'type' => 'button',
        'body' => ['text' => 'Choose an option'],
        'action' => ['buttons' => [/* ... */]],
    ]);

use Illuminate\Notifications\Notification;
use Shakil\Fast2sms\Enums\SmsRoute;
use Shakil\Fast2sms\Notifications\Messages\SmsMessage;

class OrderShipped extends Notification
{
    public function via(object $notifiable): array
    {
        return ['fast2sms'];
    }

    public function toSms(object $notifiable): SmsMessage
    {
        return SmsMessage::create("Your order #{$this->order->id} has shipped!")
            ->withRoute(SmsRoute::QUICK);
    }
}

// app/Models/User.php
public function routeNotificationForFast2sms(): string
{
    return $this->phone_number;
}

use Illuminate\Notifications\Notification;
use Shakil\Fast2sms\Notifications\Messages\WhatsAppMessage;

class OrderShippedWhatsApp extends Notification
{
    public function via(object $notifiable): array
    {
        return ['whatsapp'];
    }

    public function toWhatsApp(object $notifiable): WhatsAppMessage
    {
        return WhatsAppMessage::text("Your order #{$this->order->id} has shipped!");
    }
}

// app/Models/User.php
public function routeNotificationForWhatsapp(): string
{
    return $this->phone_number;
}

Fast2sms::quick(numbers: '9876543210', message: 'Queued message');

Fast2sms::onQueue('high-priority')
    ->onConnection('redis')
    ->quick(numbers: '9876543210', message: 'Urgent!');

// In EventServiceProvider
protected $listen = [
    \Shakil\Fast2sms\Events\SmsSent::class => [
        \App\Listeners\LogSmsSent::class,
    ],
    \Shakil\Fast2sms\Events\LowBalanceDetected::class => [
        \App\Listeners\NotifyAdminOfLowBalance::class,
    ],
];

use Shakil\Fast2sms\DataTransferObjects\SmsParameters;
use Shakil\Fast2sms\Enums\SmsRoute;
use Shakil\Fast2sms\Facades\Fast2sms;

Fast2sms::fake();

// Run code that sends SMS...
$this->post('/send-otp', ['phone' => '9876543210']);

// Assert
Fast2sms::assertSmsSent();
Fast2sms::assertSmsSentTo('9876543210');
Fast2sms::assertSmsSentWithMessage('Your OTP');
Fast2sms::assertSmsSentCount(1);
Fast2sms::assertSmsNotSent();
Fast2sms::assertSmsSentWithRoute(SmsRoute::QUICK);

// Closure-based assertion
Fast2sms::assertSmsSent(function (SmsParameters $params): bool {
    return str_contains($params->message, 'OTP');
});

// Assert nothing was sent
Fast2sms::assertNothingSent();

use Shakil\Fast2sms\Enums\WhatsAppType;

Fast2sms::assertWhatsAppSent();
Fast2sms::assertWhatsAppSentTo('919876543210');
Fast2sms::assertWhatsAppSentCount(1);
Fast2sms::assertWhatsAppSentWithType(WhatsAppType::TEXT);
Fast2sms::assertWhatsAppNotSent();

// Closure-based assertion
use Shakil\Fast2sms\DataTransferObjects\WhatsAppParameters;

Fast2sms::assertWhatsAppSent(function (WhatsAppParameters $params): bool {
    return $params->to === '919876543210'
        && $params->type === WhatsAppType::TEXT;
});

// Assert nothing was sent (SMS or WhatsApp)
Fast2sms::assertNothingSent();

// Assert total sends across both channels (counts typed SMS + WhatsApp records)
Fast2sms::assertSentCount(3);

// Assert exact total sends via raw sentMessages log entries
Fast2sms::assertSentTimes(3);

// Generic low-level assertion with optional closure (closure receives raw array payload)
Fast2sms::assertSent(fn (array $message) => $message['numbers'] === ['9876543210']);

// Assert no message matching criteria was sent
Fast2sms::assertNotSent(fn (array $message) => $message['numbers'] === ['9876543210']);

protected function tearDown(): void
{
    Fast2sms::stopFaking();
    parent::tearDown();
}

use Shakil\Fast2sms\Rules\Fast2smsPhone;

$request->validate([
    'phone' => ['

use Shakil\Fast2sms\Exceptions\ApiException;
use Shakil\Fast2sms\Exceptions\AuthenticationException;
use Shakil\Fast2sms\Exceptions\Fast2smsException;
use Shakil\Fast2sms\Exceptions\NetworkException;
use Shakil\Fast2sms\Exceptions\RateLimitException;
use Shakil\Fast2sms\Exceptions\ValidationException;

try {
    Fast2sms::quick(numbers: '9876543210', message: 'Hello!');
} catch (AuthenticationException $e) {
    // Invalid API key — check FAST2SMS_API_KEY
} catch (RateLimitException $e) {
    // Too many requests — back off and retry
} catch (ValidationException $e) {
    // Invalid input — $e->getMessage() describes the problem
} catch (ApiException $e) {
    // API returned an error — check $e->getMessage()
} catch (NetworkException $e) {
    // Network timeout or connection failure
} catch (Fast2smsException $e) {
    // Catch-all for any other package exception
}
bash
php artisan vendor:publish --tag=fast2sms-config
bash
php artisan fast2sms:events
bash
# Check balance with custom threshold
php artisan fast2sms:balance --threshold=500

# Output as JSON (for scripting/CI)
php artisan fast2sms:balance --json

# Show WABA details
php artisan fast2sms:waba

# List all events
php artisan fast2sms:events

# Generate IDE helper
php artisan fast2sms:ide-helper