PHP code example of beziworld / laravel-serwersms

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

    

beziworld / laravel-serwersms example snippets


use Beziworld\SerwerSms\Facades\SerwerSms;
use Beziworld\SerwerSms\Messages\SmsMessage;

// Shortcut
SerwerSms::sendText('500 600 700', 'Your code is 1234');

// Full builder
$result = SerwerSms::send(
    SmsMessage::create('Your order #1001 has shipped')
        ->to(['500600700', '+48600700800'])
        ->from('Shop')                          // SMS FULL; ->eco() sends SMS ECO
        ->uniqueId('order1001a', 'order1001b')  // one per recipient, returned in reports
        ->sendAt(now()->addHour())
);

$result->queued;            // number of queued messages
$result->ids();             // SerwerSMS ids of the queued messages
$result->failedMessages();  // recipients rejected individually, with errorCode / errorMessage

use Beziworld\SerwerSms\Messages\{MmsMessage, PersonalizedMessage, VoiceMessage};

SerwerSms::sendPersonalized(
    PersonalizedMessage::create()->add('500600700', 'Hi Jan')->add('600700800', 'Hi Ewa')
);

SerwerSms::sendVoice(VoiceMessage::create('Your visit is tomorrow at 10.')->to('500600700')->lector('Maja'));

$fileId = SerwerSms::uploadFile('mms', 'https://example.com/promo.jpg');
SerwerSms::sendMms(MmsMessage::create('Promo')->files($fileId)->text('-20% today')->to('500600700'));

SerwerSms::cancelScheduled(ids: ['111065e43c'], uniqueIds: ['order1001a']);

SerwerSms::reports(['id' => [$id], 'date_from' => now()->subDay()]); // list<MessageReport>
SerwerSms::report($id);                         // ?MessageReport
SerwerSms::received('eco', markAsRead: true);   // incoming messages
SerwerSms::checkNumber('500600700');            // HLR: availability, network, porting (billed)
SerwerSms::lookupNumber('500600700');           // network lookup (billed)
SerwerSms::limits()->get('full')?->remaining(); // remaining messages per type
SerwerSms::senders();  SerwerSms::addSender('NewBrand');
SerwerSms::blacklist(); SerwerSms::addToBlacklist($phone); SerwerSms::isBlacklisted($phone);
SerwerSms::groups(); SerwerSms::contacts($groupId); SerwerSms::createContact($groupId, $phone);
SerwerSms::templates();
SerwerSms::call('stats/index', [...]);          // any other HTTPS API v2 action

use Beziworld\SerwerSms\Messages\SmsMessage;
use Illuminate\Notifications\Notification;

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

    public function toSerwerSms(object $notifiable): SmsMessage|string
    {
        return SmsMessage::create("Order {$this->order->number} has shipped");
    }
}

public function routeNotificationForSerwersms(): ?string
{
    return $this->phone;
}

   use Beziworld\SerwerSms\Events\DeliveryReportReceived;
   use Beziworld\SerwerSms\Events\IncomingMessageReceived;

   Event::listen(function (DeliveryReportReceived $event) {
       $event->report->messageId;   // #SMSID#
       $event->report->status;      // DeliveryStatus::Delivered, Undelivered, Unsent, Pending
       $event->report->reason;      // #PRZYCZYNA#
   });

   Event::listen(function (IncomingMessageReceived $event) {
       $event->message->phone;      // +48500600700
       $event->message->text;
   });
   

// config/serwersms.php
'default' => env('SERWERSMS_CONNECTION', 'default'),
'connections' => [
    'default' => ['token' => env('SERWERSMS_TOKEN'), 'sender' => env('SERWERSMS_SENDER')],
    'shop' => ['token' => env('SERWERSMS_SHOP_TOKEN'), 'sender' => 'Shop'],
],

SerwerSms::connection('shop')->sendText($phone, 'Hi');

// Credentials stored per tenant
SerwerSms::using(['token' => $tenant->serwersms_token, 'sender' => $tenant->sms_sender])->send($message);

try {
    SerwerSms::sendText($phone, $text);
} catch (ApiException $e) {
    if ($e->errorCode() === 3002) {
        // SMS FULL limit exhausted
    }
}

Http::fake(['api2.serwersms.pl/*' => Http::response(['success' => true, 'queued' => 1, 'unsent' => 0])]);