PHP code example of misaf / laravel-sms-gateway

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

    

misaf / laravel-sms-gateway example snippets


use Misaf\LaravelSmsGateway\Facades\SmsGateway;

$response = SmsGateway::driver()->send([
    'message' => 'Hello',
    'receptor' => '09123456789',
]);

SmsGateway::driver('kavenegar')->send($data);                              // a named driver
SmsGateway::driver('ghasedak')->request()->post('sms/send/simple', $data); // the raw HTTP client

use Misaf\LaravelSmsGateway\Events\SmsSendFailed;

final class ReportSmsGatewayFailure
{
    public function handle(SmsSendFailed $event): void
    {
        logger()->error($event->driverName, [
            'status' => $event->response->status(),
            'body' => $event->response->body(),
        ]);
    }
}

namespace App\SmsGateways;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Misaf\LaravelSmsGateway\Drivers\SmsGatewayDriver;

final class CustomDriver extends SmsGatewayDriver
{
    public function __construct(
        string $baseUrl,
        private readonly string $token,
        int $serverTimeout,
        int $clientTimeout,
        int $retryTimes,
        int $retrySleepMilliseconds,
    ) {
        parent::__construct($baseUrl, $serverTimeout, $clientTimeout, $retryTimes, $retrySleepMilliseconds);
    }

    protected function driverName(): string
    {
        return 'custom';
    }

    /**
     * @param array<string, mixed> $data
     */
    protected function sendRequest(array $data): Response
    {
        return $this->request()->post('messages', $data);
    }

    protected function configure(PendingRequest $request): PendingRequest
    {
        return $request->withToken($this->token);
    }
}

use Illuminate\Support\Facades\Config;

SmsGateway::extend('custom', fn (): SmsGateway => new CustomDriver(
    baseUrl: Config::string('services.custom.base_url'),
    token: Config::string('services.custom.token'),
    serverTimeout: Config::integer('sms-gateway.defaults.server_timeout'),
    clientTimeout: Config::integer('sms-gateway.defaults.client_timeout'),
    retryTimes: Config::integer('sms-gateway.defaults.retry_times'),
    retrySleepMilliseconds: Config::integer('sms-gateway.defaults.retry_sleep_milliseconds'),
));

use Misaf\LaravelSmsGateway\SmsGatewayManager;

$this->callAfterResolving(
    SmsGatewayManager::class,
    fn (SmsGatewayManager $manager) => $manager->extend('custom', $factory),
);