, (*1)
Integrate SMS API with Laravel
Laravel package to provide SMS API integration. Any SMS vendor that provides REST API can be used., (*2)
Installation
Install Package
Require this package with composer:, (*3)
composer require gr8shivam/laravel-sms-api
Add Service Provider & Facade
For Laravel 5.5+
Once the package is added, the service provider and facade will be autodiscovered., (*4)
For Older versions of Laravel
Add the ServiceProvider to the providers array in config/app.php
:, (*5)
Gr8Shivam\SmsApi\SmsApiServiceProvider::class,
Add the Facade to the aliases array in config/app.php
:, (*6)
'SmsApi': Gr8Shivam\SmsApi\SmsApiFacade::class,
Publish Config
Once done, publish the config to your config folder using:, (*7)
php artisan vendor:publish --provider="Gr8Shivam\SmsApi\SmsApiServiceProvider"
Configuration
Once the config file is published, open config/sms-api.php
, (*8)
Global config
country_code
: The default country code to be used, (*9)
default
: Default gateway, (*10)
Gateway Config
Use can define multiple gateway configs like this:-, (*11)
// Gateway Configuration
'gateway_name' => [
'method' => 'GET', //Choose Request Method (GET/POST) Default:GET
'url' => 'BaseUrl', //Base URL
'params' => [
'send_to_param_name' => '', //Send to Parameter Name
'msg_param_name' => '', //Message Parameter Name
'others' => [
'param1' => '',
'param2' => '',
'param3' => '',
//More params can be added
],
],
'headers' => [
'header1' => '',
'header2' => '',
//More headers can be added
],
// 'json' => true, // OPTIONAL: Use if you want the params to be sent in JSON format instead of query params (accepts true/false)
// 'wrapper' => 'wrapper_name', // OPTIONAL: Use only if you want the JSON request to be wrapped (accepts string)
'add_code' => true, //Include Country Code (true/false)
],
Special Parameters in Gateway Config
json
Parameter
The json
parameter accepts true/false
. When true
, it sends params
as a JSON payload. It also takes care of 'Content-Type' => 'application/json'
header., (*12)
jsonToArray
Parameter
The jsonToArray
parameter accepts true/false
. When true
, it sends a single mobile number in an encapsulated array in the JSON payload. When false
, a single mobile number is sent as text. Valid only when json
parameter is true
., (*13)
wrapper
Parameter
The wrapper
is a special parameter which will be required only with some gateways. It wraps the JSON payload in the following structure:, (*14)
"wrapper_name": [
{
"message": "Message",
"to": [
"Receipient1",
"Receipient2"
]
}
]
wrapperParams
Parameter
Accepts array. Used to add custom Wrapper Parameters. Parameters can also be added while calling the smsapi()
function like smsapi()->addWrapperParams(['wrapperParam1'=>'paramVal'])->sendMessage("TO", "Message")
, (*15)
Usage
Direct Use
Use the smsapi()
helper function or SmsApi
facade to send the messages., (*16)
TO
: Single mobile number or Multiple comma-separated mobile numbers, (*17)
MESSAGE
: Message to be sent, (*18)
Using Helper function
-
Basic Usage smsapi("TO", "Message");
or smsapi()->sendMessage("TO","MESSAGE");
, (*19)
-
Adding extra parameters smsapi("TO", "Message", ["param1" => "val"]);
or smsapi()->sendMessage("TO", "Message", ["param1" => "val"]);
, (*20)
-
Adding extra headers smsapi("TO", "Message", ["param1" => "val"], ["header1" => "val"]);
or smsapi()->sendMessage("TO", "Message", ["param1" => "val"], ["header1" => "val"]);
, (*21)
-
Using a different gateway smsapi()->gateway('GATEWAY_NAME')->sendMessage("TO", "Message");
, (*22)
-
Using a different country code smsapi()->countryCode('COUNTRY_CODE')->sendMessage("TO", "Message");
, (*23)
-
Sending message to multiple mobiles smsapi(["Mobile1","Mobile2","Mobile3"], "Message");
or smsapi()->sendMessage(["Mobile1","Mobile2","Mobile3"],"MESSAGE");
, (*24)
Using SmsApi facade
-
Basic Usage SmsApi::sendMessage("TO","MESSAGE");
, (*25)
-
Adding extra parameters SmsApi::sendMessage("TO", "Message", ["param1" => "val"]);
, (*26)
-
Adding extra headers SmsApi::sendMessage("TO", "Message", ["param1" => "val"], ["header1" => "val"]);
, (*27)
-
Using a different gateway SmsApi::gateway('GATEWAY_NAME')->sendMessage("TO", "Message");
, (*28)
-
Using a different country code SmsApi::countryCode('COUNTRY_CODE')->sendMessage("TO", "Message");
, (*29)
-
Sending message to multiple mobiles SmsApi::sendMessage(["Mobile1","Mobile2","Mobile3"],"MESSAGE");
, (*30)
Use in Notifications
Setting up the Route for Notofication
Add the method routeNotificationForSmsApi()
to your Notifiable model :, (*31)
public function routeNotificationForSmsApi() {
return $this->phone; //Name of the field to be used as mobile
}
By default, your User model uses Notifiable., (*32)
Setting up Notification
Add, (*33)
use Gr8Shivam\SmsApi\Notifications\SmsApiChannel;
, (*34)
and, (*35)
use Gr8Shivam\SmsApi\Notifications\SmsApiMessage;
, (*36)
to your notification., (*37)
You can create a new notification with php artisan make:notification NOTIFICATION_NAME
, (*38)
In the via
function inside your notification, add return [SmsApiChannel::class];
and add a new function toSmsApi($notifiable)
to return the message body and parameters., (*39)
Notification example:-, (*40)
namespace App\Notifications;
use Gr8Shivam\SmsApi\Notifications\SmsApiChannel;
use Gr8Shivam\SmsApi\Notifications\SmsApiMessage;
use Illuminate\Notifications\Notification;
class ExampleNotification extends Notification
{
public function via($notifiable)
{
return [SmsApiChannel::class];
}
public function toSmsApi($notifiable)
{
return (new SmsApiMessage)
->content("Hello");
}
}
You can also use ->params(["param1" => "val"])
to add extra parameters to the request and ->headers(["header1" => "val"])
to add extra headers to the request., (*41)
Getting Response
You can get response by using ->response()
or get the Response Code using ->getResponseCode()
. For example, smsapi()->sendMessage("TO","MESSAGE")->response();
, (*42)
Support
Feel free to post your issues in the issues section., (*43)
Credits
Developed by Shivam Agarwal, (*44)
Thanks to laravel-ovh-sms & softon-sms, (*45)
License
MIT, (*46)