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 = "https://api.twilio.com/2010-04-01/Accounts/".$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_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
curl_close($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

<?php

function cspd_send_nexmo_sms($key,$secret,$to_number,$from_number,$sms_text)
{
  $url = 'https://rest.nexmo.com/sms/json?' . 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.

 

Use MSG91 SMS API

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:

http://api.msg91.com/api/sendhttp.php?route=4&sender=TESTIN&mobiles=PHONE_NUMBER&authkey=AUTH_KEY&message=Hello! This is a test message&country=0&response=json

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

<?php
$send_msg91_sms = file_get_contents("http://api.msg91.com/api/sendhttp.php?route=4&sender=TESTIN&mobiles=PHONE_NUMBER&authkey=AUTH_KEY&message=Hello! 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:

<?php
  // 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('https://api.textlocal.in/send/?' . $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  curl_close($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.


Leave a Reply

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