How to create your own SMS service website?

Ever wonder creating your own SMS service like a website, where a user can simply enter the sender number and send SMS to anyone they want.

What do you require?

  • Hosting/Server
  • Account on MSG91

MSG91 and other services provide a simple API which we could use in our custom code and start using our SMS service. This time we’re going to use MSG91.com. There are more like nexmo.com etc.

For sending SMS using any language, you’d need an SMS gateway. Most of the SMS providers these days provide an API. For the below PHP snippet, I am using MSG91 as SMS gateway.

 

function send_sms($mobile,$msg)
{
	$authKey = "XXXXXXXXXXX";
date_default_timezone_set("Asia/Kolkata");
	$date = strftime("%Y-%m-%d %H:%M:%S");
//Multiple mobiles numbers separated by comma
	$mobileNumber = $mobile;

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

//Your message to send, Add URL encoding here.
	$message = urlencode($msg);

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

//API URL
	$url="https://control.msg91.com/sendhttp.php";

// init the resource
	$ch = curl_init();
	curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData
    //,CURLOPT_FOLLOWLOCATION => true
));


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

 

You’d see that I have highlighted two lines. On the first highlighted line you need to enter your passkey and on the second you need to enter your senderID. While entering mobile number you need to specify the country code (For example, USA’s country code is 1. India’s country code is 91).

Code:

<?php
$message = "Hello World";
$mobile = "1234567890";
send_sms($mobile,$message);
?>
1

 

Back to top button
Close