How to send SMS with PHP?


This article is going to be attractive to you. In this tutorial, We are going to explain you how to send SMS in PHP with Twilio SMS API.

I expect, you are also enthusiastic to see how to send SMS with PHP? From back-end using the PHP language.

Formerly you are going to do this job in PHP, you required an SMS API provider. There are a few well-known API providers like Twilio, Nexmo, MSG91, Textlocal are occurring. With the help of SMS API you can send SMS using the PHP.

All above API will give you a few free SMS after you generate an account on their website. You can use these to check your code.

In this article, We are going to show you how to send an SMS using Twilio in php, Nexmo and MSG91 SMS API.

Send SMS in php using Twilio SMS API

First step, you need to create 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;

The, focus to see how to use our own function:

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

In the upper given code, just give your own Twilio SID, Twilio token, the number which you will get from Twilio. Next, add the phone number where you want to send SMS and then the SMS text.

Send SMS with PHP using Nexmo SMS API

After given code is our function to send SMS with PHP 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 created function, just call it and give your Nexmo API key, API secret key, the mobile number No to send SMS, from mobile number where you will get from Nexmo and SMS body text.


NOW, This will going to be easy. All you require to do, only send the MSG91 auth API and other data in URL query you can see below:! This is a test message&country=0&response=json

To send the SMS and then you will get the response in PHP:

$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 with PHP In this code

I am going to explain you is using the Textlocal Indian API. Underneath is the code sample that can send SMS to the Indian mobile 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 which you can send SMS to multiple mobile number just by adding it in “$mobile_numbers” variable by splitting up every number with a comma(,).

Like it? Share with your friends!