PHP code example of farsi / laravel-smsir

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

    

farsi / laravel-smsir example snippets


use Farsi\Smsir\Facades\Smsir;

// One message to one recipient
Smsir::send('09120000000', 'سلام!');

// The same message to many
Smsir::bulk('تخفیف ویژه امروز', ['09120000000', '09120000001']);

// A different message to each recipient, in one request
Smsir::likeTolike(['سلام علی', 'سلام مریم'], ['09120000000', '09120000001']);

// A template (verification) message — the OTP shape
Smsir::verify('09120000000', templateId: 100, ['CODE' => '1234']);

// Scheduled
Smsir::bulk('یادآوری', $mobiles, sendAt: now()->addHour());
Smsir::cancelScheduled($packId);

// config/smsir.php
'templates' => [
    'otp' => env('SMSIR_TEMPLATE_OTP'),
],

Smsir::verify('09120000000', 'otp', ['CODE' => $code]);

Smsir::reports()->byId($messageId);
Smsir::reports()->byPack($packId);
Smsir::reports()->packs(page: 1, perPage: 50);
Smsir::reports()->live(mobile: '09120000000');
Smsir::reports()->archive(from: now()->subWeek(), to: now());

Smsir::received()->latest(50);
Smsir::received()->live();
Smsir::received()->archive(from: now()->subMonth(), to: now());

Smsir::credit();   // float
Smsir::lines();    // ['30007', ...]

use Farsi\Smsir\Notifications\SmsirMessage;
use Illuminate\Notifications\Notification;

class SendOtp extends Notification
{
    public function __construct(protected string $code) {}

    public function via($notifiable): array
    {
        return ['smsir'];
    }

    public function toSmsir($notifiable): SmsirMessage
    {
        return SmsirMessage::verify('otp')->with(['CODE' => $this->code]);
    }
}

$user->notify(new SendOtp($code));

return SmsirMessage::text('سفارش شما ارسال شد.');

use Farsi\Smsir\Events\{SmsSending, SmsSent, SmsFailed};

Event::listen(SmsSent::class, function (SmsSent $event) {
    logger()->info("Sent {$event->packId} for {$event->cost} credits");
});

Event::listen(SmsSending::class, function (SmsSending $event) {
    if (! app()->isProduction()) {
        $event->payload['mobiles'] = ['09120000000'];
    }
});

use Farsi\Smsir\Exceptions\{SmsirException, InvalidApiKeyException, ConnectionFailedException};

try {
    Smsir::send('09120000000', 'سلام');
} catch (InvalidApiKeyException $e) {
    // envelope status 10
} catch (ConnectionFailedException $e) {
    // no answer at all — the message may still have been sent
} catch (SmsirException $e) {
    $e->statusCode;   // upstream status
    $e->getMessage(); // upstream Persian message, verbatim
}

Http::fake(['api.sms.ir/*' => Http::response([
    'status' => 1, 'message' => 'موفق',
    'data' => ['messageId' => 1, 'cost' => 1],
])]);

Smsir::verify('09120000000', 'otp', ['CODE' => '1234']);

Http::assertSent(fn ($request) => $request['mobile'] === '09120000000');
bash
php artisan vendor:publish --tag=smsir-config
bash
php artisan vendor:publish --tag=smsir-migrations
php artisan migrate