PHP code example of sureshhemal / laravel-sms-sri-lanka
1. Go to this page and download the library: Download sureshhemal/laravel-sms-sri-lanka 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/ */
sureshhemal / laravel-sms-sri-lanka example snippets
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Sureshhemal\SmsSriLanka\Contracts\ReceivesSmsNotifications;
class User extends Authenticatable implements ReceivesSmsNotifications
{
// ... your model code ...
/**
* Route notifications for the SMS channel.
* This method should return the phone number for SMS delivery.
*/
public function routeNotificationForSms(): ?string
{
return $this->phone_number; // or $this->telephone_number, etc.
}
}
use Sureshhemal\SmsSriLanka\Notifications\SmsNotification;
// Send to single recipient
$user->notify(new SmsNotification('Your message here'));
// Send to multiple recipients
Notification::send($users, new SmsNotification('Your message here'));
// With custom options
$notification = new SmsNotification('Your message', [
'delivery_report_request' => true,
'campaign_name' => 'Custom Campaign'
]);
$user->notify($notification);
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Sureshhemal\SmsSriLanka\Contracts\SendsSmsNotification;
class WelcomeSmsNotification extends Notification implements SendsSmsNotification, ShouldQueue
{
use Queueable;
public function via($notifiable)
{
return ['sms'];
}
/**
* Get the SMS representation of the notification.
*/
public function toSms($notifiable): string
{
return "Welcome {$notifiable->name}! Your account has been created successfully.";
}
/**
* Get the SMS options for the notification.
*/
public function smsOptions(): array
{
return [
'delivery_report_request' => true,
'campaign_name' => 'Welcome Campaign',
];
}
}
use Sureshhemal\SmsSriLanka\Contracts\SmsServiceContract;
// Inject the service
public function sendWelcomeSms(SmsServiceContract $smsService)
{
// Send to single recipient
$response = $smsService->sendSms(
to: '94701234567',
message: 'Welcome to our service!',
options: [
'campaign_name' => 'Welcome Campaign',
'delivery_report_request' => true,
]
);
// Send to multiple recipients (comma-separated phone numbers)
$response = $smsService->sendSms(
to: '94701234567,94712345678,94723456789',
message: 'Bulk message to multiple recipients!',
options: [
'campaign_name' => 'Bulk Campaign',
'delivery_report_request' => true,
]
);
return $response;
}
interface ReceivesSmsNotifications
{
/**
* Route notifications for the SMS channel.
* Return the phone number for SMS delivery.
*/
public function routeNotificationForSms(): ?string;
}
interface SendsSmsNotification
{
/**
* Get the SMS representation of the notification.
*/
public function toSms($notifiable): string;
/**
* Get the SMS options for the notification.
*/
public function smsOptions(): array;
}
interface SmsServiceContract
{
/**
* Send an SMS message
*
* @param string $to Phone number(s) - single number or comma-separated list
* @param string $message The SMS message content
* @param array $options Additional options for the SMS
* @return array The response from the SMS API
*/
public function sendSms(string $to, string $message, array $options = []): array;
}