How to send SMS in PHP?

This tutorial is going to be interesting to you. In this article, I am going to show you how to send SMS in PHP.

I hope, you are also excited to see how to send SMS programmatically from server side using the PHP programming language.

Before you are going to do this task in PHP, you need an SMS API provider. There are some well-known API providers like Twilio, Nexmo, MSG91, Textlocal are exists that you can use to send SMS using the PHP programming language.

All these API providers will give you some free SMS after you create an account on their site. You can use these to test your code.

In this tutorial, I am going to show you how to send an SMS using Twilio, Nexmo and MSG91 SMS API.


Send SMS using Twilio SMS API

First, create our own PHP function:

function send_twilio_sms($id, $token, $from, $to, $body)
$url = "".$id."/SMS/Messages";
$data = array (
    'From' => $from,
    'To' => $to,
    'Body' => $body,
$post = http_build_query($data);
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
return $y;

Now see how to use our function:

// Usage of our function to send an SMS using Twilio API in PHP
send_twilio_sms($id, $token, $from, $to, $body)

In the above code, just provide your own Twilio SID, Twilio token, the number that you will get from Twilio. After that put the phone number where you want to send SMS and then the body of SMS or SMS text.


Send SMS using Nexmo SMS API

Below is our function to send SMS using Nexmo SMS API


function cspd_send_nexmo_sms($key,$secret,$to_number,$from_number,$sms_text)
  $url = '' . http_build_query([
        'api_key' => $key,
        'api_secret' => $secret,
        'to' => $to_number,
        'from' => $from_number,
        'text' => $sms_text

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    return curl_exec($ch);


To use this function, just call it and provide your own Nexmo API key, API secret, the phone number to send SMS, from phone number that you will get from Nexmo and SMS text.



This is going to be simple. All you need to do is just send the MSG91 auth API and other data via URL query just like you can see below:! This is a test message&country=0&response=json

To send the SMS and then get the response in PHP below is the code:

$send_msg91_sms = file_get_contents("! This is a test message&country=0&response=json");
$response = json_decode($send_msg91_sms);


Also, read:


Using Textlocal SMS API to send SMS in PHP

The code I am going to show you is using the Textlocal Indian API. Below is the code snippet that can send SMS to Indian phone number:

  // Account and API details
  $apiKey = urlencode('YOUR_TEXTLOCAL_API_KEY');
  // text Message details
  $phone_numbers = urlencode('91xxxxxxxxxx,91xxxxxxxxxx');
  $sender = urlencode('TXTLCL');
  $msg = rawurlencode('Test message sent from textlocal');
  $data = 'apikey=' . $apiKey . '&numbers=' . $phone_numbers . "&sender=" . $sender . "&msg=" . $msg;
  // Send the GET request with cURL to send SMS
  $ch = curl_init('' . $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  // Get the response here
  echo $response;

You can see that you can send SMS to multiple phone number just by passing it in “$phone_numbers” variable by separating each number with a comma.

3 responses to “How to send SMS in PHP?”

  1. vince tita says:

    Hi Faruque,
    Hope you doing great.
    I have been trying to use SMS API (contact form7 sms alert plugin wordpress), I have INSTALLED the plugin the usual way on the wordpress admin dashboard,
    when click to ACTIVATE it does not activate. I have tried to research the solution in vain.
    Please can you point me to the right direction.

  2. gumuruh says:

    which one that has a free sending sms?
    i wonder if those steps you just gave above are all the commercial services, am i right?

Leave a Reply

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