How to send free sms using php with source code




To send SMS(Short Message Service) using PHP. You need some extra tool and service excluding coding and some networking special PHP library which is then responsible to send the SMS. In this blog, you will learn how to send SMS using PHP with source code is free of cost. So let's get started.

Introduction


In many Websites and application, when someone verifies the mobile phone number of forgate password then they can easily get the password or reset the password by the OTP(One Time Password). But the question arises how that Website or mobile application sends the OTP or SMS to the client mobile phone. This type of feature we will see in this blog. Many of the students have a question like

  •     how to send OTP SMS using PHP
  •     how to send SMS from a website using PHP
  •     how to send free SMS using PHP with source code

In this blog, we will focus on the above three questions and give the complete idea with the source code os that anyone can read and practice in their machine.

Requisite

  1. PHP
  2. SMS service provider
  3. XAMPP server

Level of Skill

  • the basic idea of HTML and CSS
  • beginner level of PHP

What is an SMS service provider?


The SMS service provider is a platform which gives you to send the SMS Or we can say that to send SMS we need a service provider.

Where to get Service provider?

Ans. In the online many service provider will offer to send SMS in between some are paid and other free. But in this case, we will use a free SMS or free service provider. Just click the TextLocal and register. It offers free 10 SMS after that you will pay.

Generate a api key in TextLocal

To create API in TextLocal follow the below step

  •     login to TextLocal
  •     go to setting and click API key
How to send free sms using php with source code

  •     Click "create new key"
How to send free sms using php with source code

  •     Don't edit anything just click "save the new key"
How to send free sms using php with source code

  •     you API is ready


CURL library in PHP


To send SMS using API we need a library called libcurl which give the facility to send data from one page to another page and perform some operation and get back the response data in the same page. libcurl is inbuild not need to install anything. This library gives some predefined function which will use to send the data to other API and get the result.

In this blog, we will use four CURL function i,e

    curl_init():- This function create a session for our request.
    curl_setopt():-This function take three parameter to send the api.
    curl_exec():-It execute the curl operation
    curl_close():-It close the curl after receive the data

Step 1:

    Design the layout with three fields like Mobile no. which is the receiver mobile no., Message and  Send Button.

sms.php file code 

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">

    <title>Send SMS using PHP</title>
  </head>
  <body>
    <div class="container-fluid">
      <div class="row">
        <div class="col-md-4"></div>
        <div class="col-md-4">
          
          <div class="smsbox">
            <form action="send.php" method="post"> 
              <div class="form-group">
                <label>Enter Mobile No.</label>
              <input class="form-control" type="text" name="mobile_number">
              </div>
              <div class="form-group">
                <label>Message</label>
                <textarea name="message" class="form-control"></textarea>
              </div>
              <button class="btn btn-primary" type="submit">Send</button>
            </form>
          </div

        </div>
        <div class="col-md-4"></div>
      </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
  </body>
</html>

    above code is use Bootstrap 4 to create the layout. Copy the code and paste in the editor with "sms.php" 

Step 2:

    Create a PHP file to send the SMS

<?php
// Account details
$apiKey = urlencode('7uS/vHeqiec-Uqsa8JR6op0mUjWs1QNWbY5FZ2EMS3');
// Message details
$numbers = $_POST["mobile_number"]; //receiver mobile no.
$sender = urlencode('TXTLCL'); //sender name
$message = rawurlencode($_POST["message"]);//message

// Prepare data for POST request
$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
 
// Send the POST request with cURL
$ch = curl_init('https://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
echo $response;
?>
 
    copy the above code and paste in the editor with "send.php"

Explanation of above code


  •     API key: store the API of which you create in TextLocal. copy and past here
  •     $numbers and $message get the data which will come from the send.php file.
  •     $sender: This variable contain the name of the sender
  •     $data: It is an array which stores all the information coming from the sms.php
  •     $ch = curl_init('https://api.textlocal.in/send/') : create the session 
  •     curl_setopt($ch, CURLOPT_POST, true) : it take three-parameter $ch is the session, CURLOPT_POST indicate the data send through post or get. true means post method use false means Get method to use.
  •     curl_setopt($ch, CURLOPT_POSTFIELDS, $data):- This function takes the data of the fields and passes to the API.
  •     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true):- If this function is false then the response data is print in the website and if true we perform some operation of response data.

    Now at the last, we print the response data which will come from the service provider



    

2 Comments

  1. I'm finding myself reading and commenting for far too long once again. But who cares, it was still a good time!
    Bulk Sms Service In Chennai

    ReplyDelete
  2. Hi,
    when i' send show the error:

    {"errors":[{"code":3,"message":"Invalid login details"}],"status":"failure"}

    ReplyDelete
Previous Post Next Post

Recent Posts