PHP code example of simply-connect / laravel

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

    

simply-connect / laravel example snippets


use SimplyConnect\Laravel\Facades\SimplyConnect;

$receipt = SimplyConnect::sms()
    ->to('+48500100200')
    ->text('Your order is ready for collection.')
    ->send();

$receipt = SimplyConnect::sms()
    ->to($customer->phone_number)
    ->text("Order {$order->number} has been shipped.")
    ->send();

$receipt->messageId;
$receipt->dispatchId;
$receipt->commandId;
$receipt->status;          // MessageStatus::Queued
$receipt->idempotencyKey;  // generated for this request
$receipt->correlationId;   // useful when contacting support

'endpoints' => [
    'support' => env('SIMPLY_CONNECT_SUPPORT_ENDPOINT_ID'),
    'notifications' => env('SIMPLY_CONNECT_NOTIFICATIONS_ENDPOINT_ID'),
],

SimplyConnect::sms()
    ->via('support')
    ->to('+48500100200')
    ->text('We will call you back within 15 minutes.')
    ->send();

$endpoints = SimplyConnect::endpoints();

foreach ($endpoints as $endpoint) {
    echo $endpoint->name;
    echo $endpoint->phoneNumber;
    echo $endpoint->enabled ? 'ready' : 'disabled';
}

use SimplyConnect\Laravel\Contracts\HasSmsNumber;

final class Customer implements HasSmsNumber
{
    public function smsNumber(): string
    {
        return $this->phone_number;
    }
}

SimplyConnect::sms()
    ->to($customer)
    ->text('Thank you for registering.')
    ->send();

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use SimplyConnect\Laravel\Notifications\SimplyConnectChannel;
use SimplyConnect\Laravel\Notifications\SmsMessage;

final class OrderShipped extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(private readonly string $orderNumber) {}

    public function via(object $notifiable): array
    {
        return [SimplyConnectChannel::class];
    }

    public function toSimplyConnect(object $notifiable): SmsMessage
    {
        return SmsMessage::make("Order {$this->orderNumber} has been shipped.")
            ->via('notifications')
            ->withIdempotencyKey("order-{$this->orderNumber}-shipped");
    }
}

public function routeNotificationForSimplyConnect(): string
{
    return $this->phone_number;
}

$customer->notify(new OrderShipped($order->number));

use SimplyConnect\Laravel\Contracts\SimplyConnectClient;

final class SendVerificationCode
{
    public function __construct(private SimplyConnectClient $simplyConnect) {}

    public function handle(string $phone, string $code): void
    {
        $this->simplyConnect
            ->sms()
            ->to($phone)
            ->text("Your verification code is {$code}.")
            ->send();
    }
}

use SimplyConnect\Laravel\Enums\MessageStatus;

$message = SimplyConnect::message($receipt->messageId);

if ($message->status === MessageStatus::Delivered) {
    // The gateway reported successful delivery.
}

if ($message->hasFailed()) {
    // FAILED, UNKNOWN or DEAD_LETTER needs application-specific handling.
}

foreach ($message->statusHistory as $event) {
    logger()->info('SMS status evidence', [
        'status' => $event->status->value,
        'source' => $event->source,
        'error_code' => $event->errorCode,
        'occurred_at' => $event->occurredAt,
    ]);
}

SimplyConnect::sms()
    ->to($customer->phone_number)
    ->text('Your invoice is available.')
    ->withIdempotencyKey("invoice-{$invoice->id}-available")
    ->send();

use SimplyConnect\Laravel\Exceptions\UnknownOutcomeException;

try {
    $receipt = SimplyConnect::sms()
        ->to($phone)
        ->text($text)
        ->send();
} catch (UnknownOutcomeException $exception) {
    RetrySms::dispatch($phone, $text, $exception->idempotencyKey);
}

use SimplyConnect\Laravel\Exceptions\AuthenticationException;
use SimplyConnect\Laravel\Exceptions\RateLimitException;
use SimplyConnect\Laravel\Exceptions\ValidationException;

try {
    $receipt = SimplyConnect::sms()->to($phone)->text($text)->send();
} catch (AuthenticationException $exception) {
    // Invalid/expired key, missing scope or forbidden endpoint.
} catch (ValidationException $exception) {
    // Invalid recipient or request content.
} catch (RateLimitException $exception) {
    $exception->retryAfter;    // seconds, when supplied by the API
    $exception->correlationId;
}

use SimplyConnect\Laravel\Data\OutgoingSms;
use SimplyConnect\Laravel\Facades\SimplyConnect;

SimplyConnect::fake();

$this->post('/orders/1842/notify');

SimplyConnect::assertSmsSentTo(
    '+48500100200',
    fn (OutgoingSms $sms): bool => $sms->endpoint === 'notifications'
        && str_contains($sms->text, '1842'),
);

SimplyConnect::assertSmsSentCount(1);

SimplyConnect::connection('client-a')
    ->sms()
    ->to('+48500100200')
    ->text('Hello from client A.')
    ->send();
bash
php artisan vendor:publish --tag=simply-connect-config