Sending SMS in Laravel application with nexmo package fastly

Sending SMS in Laravel application with nexmo package fastly

Sending SMS in Laravel application with nexmo package

Hello guys, In this quick tutorial I am going to tell you. How to send Sms in Laravel using nexmo package. In many projects, you may need to integrate some API’s to send msgs to the people to perform the operation like send notifications on Mobile. So How can you do that here we will discuss How to send Sms in laravel application.

First You’ll need a Nexmo account, the Nexmo CLI (Command Line Interface) and setup.

Introduction and Installation.

let’s create our Laravel SMS application:

Simply install laravel with composer.


composer create-project --prefer-dist laravel/laravel laravel-project
cd laravel-project


Next, let’s add the Nexmo Laravel package to composer.json.

"require": {
    ...,
    "nexmo/laravel": "dev-master as 1.0",
    "nexmo/client": "dev-master as 1.0"
},

Hit the composer update command to install the packages and dependencies.

$ composer update

$ php artisan vendor:publish

Add the Nexmo Service Provider to your app.php config:

'providers' => [
    ...,
    Nexmo\Laravel\NexmoServiceProvider::class
]

Now next is add your API settings like app key and secret to config/nexmo.php file.

‘api_key’ => ‘API_KEY’,
‘api_secret’ => ‘API_SECRET’,

you can find api key and api secret from your nexmo dashboard.

Now question is, How to send SMS with nexmo?

Sending an SMS

Open your route->web.php file and add the following route.

Route::get('/sms/send/{to}', function(\Nexmo\Client $nexmo, $to){
    $message = $nexmo->message()->send([
        'to' => $to,
        'from' => '@shouters',
        'text' => 'Sending SMS from Shouters'
    ]);
    Log::info('sent message: ' . $message['message-id']);
});

Here You’ll notice that in the above code the from value is set to a string with the value of @shouters. This will work in the UK, but may not work in other countries. It really depends on the country.

Listing, Searching and Actually send sms

TheNexmo CLI. We can use this to working with phone numbers. Here you need to do the settings like API key and API secret. you can install it by the following command.

 $ npm install -g nexmo-cli

 $ nexmo setup API_KEY API_SECRET

 

you can also list the numbers. just hit the command like this

$ nexmo number:list

Also , you can search it by countries.

$ nexmo number:search US

Sending an SMS

Replace the from value with the real number you’ve just purchased or load the number in using env(‘NEXMO_NUMBER’) like this if you update your code to use the number you’ve just purchased.

Route::get('/sms/send/{to}', function(\Nexmo\Client $nexmo, $to){
    $message = $nexmo->message()->send([
        'to' => $to,
        'from' => env('NEXMO_NUMBER'),
        'text' => 'Sending SMS from Shouters'
    ]);
    Log::info('sent message: ' . $message['message-id']);
});

Then just start your laravel application server

$ php artisan serve
Laravel development server started on http://localhost:8000/

you can hit URL like http://localhost:8000/sms/send/YOUR_NUMBER where YOUR_NUMBER should be replaced with your real number including the country code in an e.164 format, the same format that has been shown in all the examples above.

Krishna Gupta

Krishna Ballabh Gupta is a Gold Medalist Full Stack Developer & Algorithm Designer and Founder of TechnologyShouters. He generally Blogs about Python, JavaScript, jQuery, AngularJS, Database, PHP, AJAX and something that interesting.

Related Posts

Create Account



Log In Your Account



1Subscribe to Our Live Updates.