10-Apr-2023
.
Admin
Hello Friends,
Today, I would like to show you the laravel nexmo send sms example tutorial. let’s discuss how to send sms using nexmo in laravel. let’s discuss laravel sms notification nexmo. I explained simply step-by-step laravel nexmo message. So, let's follow a few steps to create an example of sending sms using nexmo in laravel.
In this example, I will give you a very simple example of sending SMS using nexmo/vonage API in laravel app. you can easily use this code in laravel 6, laravel 7, laravel 8 and laravel 9 app.
You have just to follow the below step and you will get the layout as below:
Step 1: Install Laravel
first of all we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Nexmo Account
First you need to create an account on nexmo. then you can easily get the client id and secret.
Create Account from here: https://dashboard.nexmo.com/sign-in.
you can register and get the client id and secret as below:
Add on env file as like bellow:
.env
NEXMO_KEY=XXXXX
NEXMO_SECRET=XXXXXXXXXXX
Step 3: Install nexmo/client Package
In this step, we need to install nexmo/client composer package to use nexmo API. so let's run below command:
composer require nexmo/client
Step 4: Create Route
now we will create one route for calling our example, so let's add a new route to the web.php file as below:
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NexmoSMSController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('sendSMS', [NexmoSMSController::class, 'index']);
Step 5: Create Controller
in this step, we will create NexmoSMSController and write send SMS logic, so let's add a new route to the web.php file as below:
app/Http/Controllers/NexmoSMSController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Exception;
class NexmoSMSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
try {
$basic = new \Nexmo\Client\Credentials\Basic(getenv("NEXMO_KEY"), getenv("NEXMO_SECRET"));
$client = new \Nexmo\Client($basic);
$receiverNumber = "91846XXXXX";
$message = "This is testing from NiceSnippets.com";
$message = $client->message()->send([
'to' => $receiverNumber,
'from' => 'Vonage APIs',
'text' => $message
]);
dd('SMS Sent Successfully.');
} catch (Exception $e) {
dd("Error: ". $e->getMessage());
}
}
}
Now you can run and check.
I hope it can help you...
#Laravel