newHelpline: +91-22-26201606 Ext.325/315. +91-22-26201604 Ext.633.
Mobile Seva

How to write PHP Client for accessing HTTP interface of SMS Gateway?

Note: For using HTTP interface of SMS Gateway you need Transport Layer Security(TLS) protocol version TLSv1.1 or TLSv1.2(the recommended and latest one)

Steps to check whether your system supports TLSv1.1 or TLSv1.2:
1) Run the following command on your system
curl --tlsv1.2 https://msdgweb.mgov.gov.in/esms/sendsmsrequest
2) If you get message "GET Method Not Supported" then it is SUCCESS!!! your system supports TLSv1.2 and you can use the below code by uncommenting/commenting specified lines in the below given code
3) If step 1 is not successful then run the following command
curl --tlsv1.1 https://msdgweb.mgov.gov.in/esms/sendsmsrequest
4) If you get message "GET Method Not Supported" then it is SUCCESS!!! your system supports TLSv1.1 and you can use the below code by uncommenting/commenting specified lines in the below given code
5) If step 1 and 3 are not successful you need to upgrade your openssl libraries for system to suppot TLSv1.1 or TLSv1.2

Click to download the PHP code file(Size:8KB)
 $value) {
   $fields .= $key . '=' . urlencode($value) . '&';
   }
   rtrim($fields, '&');
   $post = curl_init();
   //curl_setopt($post, CURLOPT_SSLVERSION, 5); // uncomment for systems supporting TLSv1.1 only
   curl_setopt($post, CURLOPT_SSLVERSION, 6); // use for systems supporting TLSv1.2 or comment the line
   curl_setopt($post,CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($post, CURLOPT_URL, $url);
   curl_setopt($post, CURLOPT_POST, count($data));
   curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
   curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec($post); //result from mobile seva server
   echo $result; //output from server displayed
   curl_close($post);
   }

  //function to send unicode sms by making http connection
   function post_to_url_unicode($url, $data) {
   $fields = '';
   foreach($data as $key => $value) {
   $fields .= $key . '=' . urlencode($value) . '&';
   }
   rtrim($fields, '&');
   
   $post = curl_init();
   //curl_setopt($post, CURLOPT_SSLVERSION, 5); // uncomment for systems supporting TLSv1.1 only
   curl_setopt($post, CURLOPT_SSLVERSION, 6); // use for systems supporting TLSv1.2 or comment the line
   curl_setopt($post,CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($post, CURLOPT_URL, $url);  
   curl_setopt($post, CURLOPT_POST, count($data));
   curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
   curl_setopt($post, CURLOPT_HTTPHEADER, array("Content-Type:application/x-www-form-urlencoded"));
   curl_setopt($post, CURLOPT_HTTPHEADER, array("Content-length:"
  . strlen($fields) ));
   curl_setopt($post, CURLOPT_HTTPHEADER, array("User-Agent:Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"));
   curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
   echo $result = curl_exec($post); //result from mobile seva server
   curl_close($post);
   }

   //function to convert unicode text in UTF-8 format
   function string_to_finalmessage($message){
   $finalmessage="";
   $sss = "";
   for($i=0;$i= 128)
   { //otherwise 0xxxxxxx
   if ($code < 224) $bytesnumber = 2;//110xxxxx
   else if ($code < 240) $bytesnumber = 3; //1110xxxx
   else if ($code < 248) $bytesnumber = 4; //11110xxx
   $codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) -
  ($bytesnumber > 3 ? 16 : 0);
   for ($i = 2; $i <= $bytesnumber; $i++) {
   $offset ++;
   $code2 = ord(substr($string, $offset, 1)) - 128;//10xxxxxx
   $codetemp = $codetemp*64 + $code2;
   }
   $code = $codetemp;

   }
   return $code;
   }
 
  //Function to send single sms
   function sendSingleSMS($username,$encryp_password,$senderid,$message,$mobileno,$deptSecureKey){
   $key=hash('sha512',trim($username).trim($senderid).trim($message).trim($deptSecureKey));
   
   $data = array(
   "username" => trim($username),
   "password" => trim($encryp_password),
   "senderid" => trim($senderid),
   "content" => trim($message),
   "smsservicetype" =>"singlemsg",
   "mobileno" =>trim($mobileno),
   "key" => trim($key)
   
   
   );
   post_to_url("https://msdgweb.mgov.gov.in/esms/sendsmsrequest",$data); //calling post_to_url to send sms
   }

   //Function to send otp sms
   function sendOtpSMS($username,$encryp_password,$senderid,$message,$mobileno,$deptSecureKey){
   $key=hash('sha512',trim($username).trim($senderid).trim($message).trim($deptSecureKey));
   
   $data = array(
   "username" => trim($username),
   "password" => trim($encryp_password),
   "senderid" => trim($senderid),
   "content" => trim($message),
   "smsservicetype" =>"otpmsg",
   "mobileno" =>trim($mobileno),
   "key" => trim($key)
   );
   post_to_url("https://msdgweb.mgov.gov.in/esms/sendsmsrequest",$data); //calling post_to_url to send otp sms
   }
 

  //function to send bulk sms
   function sendBulkSMS($username,$encryp_password,$senderid,$message,$mobileNos,$deptSecureKey){
   $key=hash('sha512', trim($username).trim($senderid).trim($message).trim($deptSecureKey));   
   
   $data = array(
   "username" => trim($username),
   "password" => trim($encryp_password),
   "senderid" => trim($senderid),
   "content" => trim($message),
   "smsservicetype" =>"bulkmsg",
   "bulkmobno" =>trim($mobileNos),
   "key" => trim($key)
   );
   post_to_url("https://msdgweb.mgov.gov.in/esms/sendsmsrequest",$data); //calling post_to_url to send bulk sms
   }

   //function to send single unicode sms
   function sendSingleUnicode($username,$encryp_password,$senderid,$messageUnicode,$mobileno,$deptSecureKey){
    $finalmessage=string_to_finalmessage(trim($messageUnicode));
   $key=hash('sha512',trim($username).trim($senderid).trim($finalmessage).trim($deptSecureKey));
   
   $data = array(
   "username" => trim($username),
   "password" => trim($encryp_password),
   "senderid" => trim($senderid),
   "content" => trim($finalmessage),
   "smsservicetype" =>"unicodemsg",
   "mobileno" =>trim($mobileno),
   "key" => trim($key)
   );

  post_to_url_unicode("https://msdgweb.mgov.gov.in/esms/sendsmsrequest",$data); //calling post_to_url_unicode to send single unicode sms
   }

   //function to send bulk unicode sms
   function sendBulkUnicode($username,$encryp_password,$senderid,$messageUnicode,$mobileNos,$deptSecureKey){
   $finalmessage=string_to_finalmessage(trim($messageUnicode));
   $key=hash('sha512',trim($username).trim($senderid).trim($finalmessage).trim($deptSecureKey));
   
   $data = array(
   "username" => trim($username),
   "password" => trim($encryp_password),
   "senderid" => trim($senderid),
   "content" => trim($finalmessage),
   "smsservicetype" =>"unicodemsg",
   "bulkmobno" =>trim($mobileNos),
   "key" => trim($key)
   );
  post_to_url_unicode("https://msdgweb.mgov.gov.in/esms/sendsmsrequest",$data); //calling post_to_url_unicode to send bulk unicode sms
  }
  
  //function to send single unicode otp sms
   function sendUnicodeOtpSMS($username,$encryp_password,$senderid,$messageUnicode,$mobileno,$deptSecureKey){
    $finalmessage=string_to_finalmessage(trim($messageUnicode));
   $key=hash('sha512',trim($username).trim($senderid).trim($finalmessage).trim($deptSecureKey));
   
   $data = array(
   "username" => trim($username),
   "password" => trim($encryp_password),
   "senderid" => trim($senderid),
   "content" => trim($finalmessage),
   "smsservicetype" =>"unicodeotpmsg",
   "mobileno" =>trim($mobileno),
   "key" => trim($key)
   );

  post_to_url_unicode("https://msdgweb.mgov.gov.in/esms/sendsmsrequest",$data); //calling post_to_url_unicode to send single unicode sms
   }
?>