Integrate and send SMS using SMS API in codeigniter

In this post, I would like to show how to send sms using sms api in codeigniter.SMS plays most important role in a web application.Password recover, reminders, OTP and for lots of other texts, we uses send sms for the same. In this tutorial i am using msgclub sms api and codeigniter.
Follow me step by step i am going to show configuration of sms api.

Since, it may be require to send sms from any where of our web application we should place our send sms function in one controller inside application/core directory and can extend this controller wherever it will require.

1. Create controller inside application/core directory called MY_Controller.php. Find below the code.

MY_Controller.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
 
class MY_Controller extends CI_Controller
 {
    public function __construct()
    {
        parent:: __construct();
    }
    public function sendMobileSms($params) {
        $mobileNumber= $params['to']; /*Separate mobile no with commas */
        $message= $params['message']; /* message */
        $senderId= "abcd"; /* Sender ID */
        $serverUrl="msg.msgclub.net";
        $authKey= ""; /* Authentication key (get from sms service provider)*/ 
        $route="1";
        $this->sendsmsGET($mobileNumber,$senderId,$route,$message,$serverUrl,$authKey);
    }
      public function sendsmsGET($mobileNumber,$senderId,$routeId,$message,$serverUrl,$authKey)
      {
		  $route = "default";
		  $getData = 'mobileNos='.$mobileNumber.'&message='.urlencode($message).'&senderId='.$senderId.'&routeId='.$routeId;
		  /* API URL */
		  $url="http://".$serverUrl."/rest/services/sendSMS/sendGroupSms?AUTH_KEY=".$authKey."&".$getData;
		  /* init the resource */
		  $ch = curl_init();
		  curl_setopt_array($ch, array(
		  CURLOPT_URL => $url,
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_SSL_VERIFYHOST => 0,
		   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);
		  return $output;
      }
 }
?> 

2. Create controller inside application/controller and Extend MY_Controller class within this controller.Here is how codes looks.

SendsmsController.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
 
 class SendsmsController extends MY_Controller 
 {
    public function __construct()
    {
        parent:: __construct();
    }
	  public function index()
	  {
        $params['to'] = "123456"; /* Separate mobile number(s) with commas */
        $params['message'] = "Hello,\nWelcome to TechAllType.Your account now active.You can now login to the account.";
        $this->user_model->sendMobileSms($params);
	  }

}
?>

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: ,