PHP Script to Send SMS From Website (curl method)

By | June 17, 2016

There are countless amazing things you can do with PHP, one such thing is sending sms. With the help of third party API we can send free sms using php or whichever language that is provided. In this tutorial we will provide a php script to send sms using curl. There are various major text messaging services for this tutorial I will be using msg91.com.

So you can sign up your account on msg91.com. After this once you login to your dashboard at the bottom there is API option you can generate your API key. In order to send SMS we need an API key. To understand more about the API you can go through the documentation. Here’s a php script to send sms.

PHP Script to Send SMS

PHP Script to Send SMS from Website



<?php

//Authentication key
$authKey = "Your API key";

//Multiple mobiles numbers separated by comma
$mobileNumber = "XXXXXXXXXX"; //Enter the phone numbers

//Sender ID,While using route4 sender id should be 6 characters long.
$senderId = "xxxxxx"; // This should be alphabetic

//Your message to send, Add URL encoding here.
$message = urlencode("TechPlugg says Hello!");

//Define route 
$route = "4";
//Prepare you post parameters
$postData = array(
 'authkey' => $authKey,
 'mobiles' => $mobileNumber,
 'message' => $message,
 'sender' => $senderId,
 'route' => $route
);

//API URL
$url="http://api.msg91.com/api/sendhttp.php";

$ch = curl_init();
 curl_setopt_array($ch, array(
 CURLOPT_URL => $url,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => $postData
 
));


//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);


//get response
$output = curl_exec($ch);

//Print error if any
if(curl_errno($ch))
{
 echo 'error:' . curl_error($ch);
}

curl_close($ch);

echo $output;
?>

Note: msg91 is used for sending SMS in India only. It is not free but you can send few free sms only for testing purposes.

Also check Send Email from GMAIL SMTP server using PHP.

Don’t miss Firebase API tutorial with PHP

I hope this tutorial was easy. If you have any doubts you can leave them in comments. In the upcoming post I will share how to send sms using Twilio.