Send SMS from Laravel

Laravel's Http facade is all you need. Put the credentials in config/services.php, wrap the API in a small class and, if you send notifications, add a notification channel.

Configuration

// .env
BUDGETSMS_USERNAME=USERNAME
BUDGETSMS_USERID=USERID
BUDGETSMS_HANDLE=HANDLE
BUDGETSMS_FROM=MyApp

// config/services.php
'budgetsms' => [
    'username' => env('BUDGETSMS_USERNAME'),
    'userid' => env('BUDGETSMS_USERID'),
    'handle' => env('BUDGETSMS_HANDLE'),
    'from' => env('BUDGETSMS_FROM', 'MyApp'),
],

The service class

<?php
// app/Services/BudgetSms.php
namespace App\Services;

use Illuminate\Support\Facades\Http;
use RuntimeException;

class BudgetSms
{
    /** Sends an SMS and returns the message id; throws RuntimeException("ERR <code>") on failure. */
    public function send(string $to, string $message, ?string $from = null): int
    {
        return (int) strtok($this->call('sendsms', ['from' => $from ?? config('services.budgetsms.from'), 'to' => $to, 'msg' => $message]), ' ');
    }

    public function credit(): float
    {
        return (float) $this->call('checkcredit');
    }

    /** Current network of a number: [mcc, mnc, price] */
    public function lookup(string $number): array
    {
        return explode(' ', $this->call('np', ['to' => $number]));
    }

    public function status(int $smsId): int
    {
        return (int) $this->call('checksms', ['smsid' => $smsId]);
    }

    private function call(string $endpoint, array $params = []): string
    {
        $config = config('services.budgetsms');
        $response = Http::timeout(20)->get('https://api.budgetsms.net/' . $endpoint . '/', [
            'username' => $config['username'], 'userid' => $config['userid'], 'handle' => $config['handle'],
        ] + $params);
        $body = trim($response->body());
        if (!$response->successful() || !str_starts_with($body, 'OK')) {
            throw new RuntimeException('BudgetSMS: ' . $body);   // "ERR 1001" etc., see the error code list
        }
        return trim(substr($body, 2));
    }
}

Use it anywhere through the container: app(BudgetSms::class)->send('31612345678', 'Your code is 482913');. Replace sendsms with testsms in your tests: it is a simulator that answers in the same format; no SMS is sent and nothing is charged (random message id and price, about one in five calls a random ERR 2xxx).

A notification channel

<?php
// app/Notifications/Channels/BudgetSmsChannel.php
namespace App\Notifications\Channels;

use App\Services\BudgetSms;
use Illuminate\Notifications\Notification;

class BudgetSmsChannel
{
    public function __construct(private BudgetSms $sms) {}

    public function send(object $notifiable, Notification $notification): void
    {
        $to = $notifiable->routeNotificationFor('budgetsms');   // the user's mobile number in international format
        if ($to) {
            $this->sms->send($to, $notification->toBudgetSms($notifiable));
        }
    }
}

// in a notification:
public function via(object $notifiable): array { return [\App\Notifications\Channels\BudgetSmsChannel::class]; }
public function toBudgetSms(object $notifiable): string { return 'Your verification code is ' . $this->code; }

// on the User model:
public function routeNotificationForBudgetsms(): ?string { return $this->mobile; }

Delivery reports

Add a route for Push DLR and enter its URL in the control panel; exclude it from CSRF protection, as the request comes from us:

// routes/web.php (or routes/api.php, which has no CSRF middleware)
Route::get('/sms/dlr', function (\Illuminate\Http\Request $request) {
    // $request->integer('id') = message id, ->integer('status') 1 = delivered, 2 and 3 = failed, ->integer('date') = unix timestamp
    \App\Models\SmsMessage::where('sms_id', $request->integer('id'))->update(['status' => $request->integer('status')]);
    return response('OK');
});