How can you send SMS using PHP?

How can you send SMS using PHP?

You'll find this tutorial intriguing. Here, I'll show you how to use PHP to send an SMS.

The PHP programming language is used to deliver SMS programmatically from the server side.

You'll need an SMS API provider before you can begin this process in PHP. It is possible to send SMS using the PHP programming language with the help of well-known API providers like Twilio, Nexmo, MSG91, Textlocal.

Creating an account on any of these API providers will result in free SMS. Use them to run your code through its paces.

Here, I'll teach you how to send an SMS using the Twilio, Nexmo, and MSG91 APIs.

Send sms with the Twilio SMS API Using PHP

To begin, we'll write 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 you have to call your function for use see below php code

send_twilio_sms($id, $token, $from, $to, $body)

Just substitute your own Twilio SID, Twilio token, or the unique number provided by Twilio, in the code above. To send an SMS, first enter the recipient's phone number, followed by the SMS text and then the body.

Use the Nexmo SMS API to send an SMS

Using the Nexmo SMS API, we've created a function to send an SMS.

<?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);
}

?>

Use this method by calling it with your personal Nexmo API key and secret along with the phone number from which you want to send SMS messages, as well as the SMS text itself.

Use MSG91's SMS API

It's going to be a cinch to do this. Sending the MSG91 auth API and other information via URL query is all you need to accomplish.

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

Here's the PHP code for sending an SMS and receiving a response:

<?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);
?>

 

Sending SMS messages in PHP using the Textlocal SMS API

Textlocal Indian API is used in the code I'm going to show you. The following code snippet can be used to send an Indian phone number an SMS:

<?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;
?>

Using the "$phone_numbers" variable and a comma to separate each number, you can send SMS to several phone numbers.

 

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

About Author
Recent Articles