Integrate SMS Sending Laravel

📩 How to Integrate an SMS Sending Service in Laravel using notify.lk SMS Gateway.

In this tutorial, you’ll learn how to integrate a custom SMS sending service into your Laravel application. We’ll move an the sms folder to app/Services, create a reusable SmsService class, and use it within a new SmsController. Finally, we’ll set up a route to test the SMS functionality via a browser. By the end, your Laravel app will have a clean structure with modular SMS logic, ready for production use.

Download Code : https://github.com/notifylk/notify-php/blob/master/README.md

1. Place the sms Folder in app/Services

Move your sms folder (with autoload.php) to the app/Services directory in your Laravel project.

2. Create the SmsService Class

This class will encapsulate the SMS sending logic.

app/Services/SmsService.php

<?php

namespace App\Services;

require_once base_path('app/Services/sms/autoload.php');

3. Create the SmsController

This controller will use the SmsService to send an SMS.

app/Http/Controllers/SmsController.phpv1

<?php

namespace App\Http\Controllers;

use App\Services\SmsService;

4. Define the Route

Add a route in routes/web.php to trigger the SMS sending functionality.

routes/web.php

use App\Http\Controllers\SmsController;

Route::get('/send-sms', [SmsController::class, 'send']);

5. Folder Structure

Your Laravel project should now have the following structure:

app/
    Services/
        sms/
            autoload.php
        SmsService.php
    Http/
        Controllers/
            SmsController.php
routes/
    web.php

Code :

<?php

namespace App\Http\Controllers;

use App\Services\SmsService;

class SmsController extends Controller
{
    public function send()
    {
        $smsService = new SmsService();

        $user_id = "245343"; // Replace with your API User ID
        $api_key = "xfsdnghrtE"; // Replace with your API Key
        $message = "Hello, this is a test message!";
        $to = "94714676522"; // Replace with the recipient's phone number
        $sender_id = "NotifyDEMO"; // Replace with your sender ID

        $response = $smsService->sendSMS($user_id, $api_key, $message, $to, $sender_id);

        return $response;
    }
}

<?php

namespace App\Services;

require_once base_path('app/Services/sms/autoload.php');

use NotifyLk\Api\SmsApi;

class SmsService
{
    protected $api_instance;

    public function __construct()
    {
        $this->api_instance = new SmsApi();
    }

    public function sendSMS($user_id, $api_key, $message, $to, $sender_id)
    {
        try {
            $response = $this->api_instance->sendSMS(
                $user_id,
                $api_key,
                $message,
                $to,
                $sender_id
            );
            return $response;
        } catch (\Exception $e) {
            return 'Exception when calling SmsApi->sendSMS: ' . $e->getMessage();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *