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.


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.


  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">
    <!-- 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="">

    <title>Send SMS using PHP</title>
    <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 class="form-group">
                <textarea name="message" class="form-control"></textarea>
              <button class="btn btn-primary" type="submit">Send</button>

        <div class="col-md-4"></div>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>

    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

// 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('');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($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('') : 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



  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

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

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


  3. Append the carrier's email domain to the end of the number. This example uses only three possible carriers.
    $recipient = $_GET['pnumber'];


    case "verizon":

    $recipient .= "";


    case "att":

    $recipient .= "";


    case "tmobile":

    $recipient .= "";




Previous Post Next Post

Recent Posts