How to send SMS to multiple recipients using PHP Codeigniter3

Send SMS to multiple recipients

Today we are going to learn How to Send SMS to Multiple Recipients Using PHP Codeigniter. Before that let us know what are the requirements we need to send text messages.

Requirements

  1. SMS Gateway Account.
  2. API URL To Send Message.
  3. Working Mobile Number.
  4. Message

To Send SMS to multiple recipients in PHP we use CURL, where CURL is a PHP library used to get data from the server and transfer data to the server.

Here to send text message we use the API URL provided by the SMS Gateway and so we use CURL to send messages to multiple devices as CURL is also used to Perform API.

First we need to setup an account in your preferred SMS Gateway and complete the verification process, Before the verification process was very easy and anyone were be able to send SMS to multiple numbers.

But now the verification process is changed as too much of spamming and fake SMS are sent, So you need to verify your account and messages before using the API.

Send SMS to multiple recipients

 Steps to send SMS through Codeigniter 

STEP 1 : 

Create a PHP file with name Users.php in applications/controller and copy the below code.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function  __construct() 
	{ 
		parent:: __construct();
		error_reporting(E_ALL ^ E_NOTICE);  
		$this->load->model(array('sms_model'));
	} 
	
	/* * *********************************************************************
	 * * Function name : index
	 * * Developed By : Tejaswi
	 * * Purpose  : This function used for SMS
	 * * Date : 09 August 2021
	 * * **********************************************************************/
	public function index()
	{	
        $fields = array('name','mobile');
        $whereCon = 'mobile != ""'; 

		$usersData = $this->sms_model->getData($fields,'users',$whereCon);

		$totalUsers = count($usersData );
        if($totalUsers > 0):
       	  foreach($totalUsers as $users):
       		$param['mobile'] 	=	$users['mobile'];
       		$param['name'] 		=	$users['name'];

            $message = 'Hello '.$users['name'].' Thankyou for visiting us.';

       		$this->sms_model->sendSMS($params,$message );
       	  endforeach;
        endif;

		$this->load->view('users',array(),$data);
	}
STEP 2 : 

Now create a model with name sms_model and copy the below code. Here i am using a single model to get data and send sms. You can use multiple models like separate model for sms related and separate model for CRUD data.

  • The first function getData is used to fetch data from users table whose mobile is not empty.
  • The second function sendSMS is used to send sms  to multiple recipients one each time.
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
class Common_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct(); 
		$this->load->database(); 
	}
	
	
	/***********************************************************************
	** Function name: getData
	** Developed By: Tejaswi
	** Purpose: This function used for get All Data
	** Date : 09 August 2021
	************************************************************************/
	public function getData($fields=array(),$tableName='',$whereCon='')
	{  
		if($fields):$this->db->select($fields);else:$this->db->select('*');endif;
		$this->db->from($tableName);	
		$this->db->where($whereCon);
		
		$query = $this->db->get();
		//echo $this->db->last_query();die;
		if($query->num_rows() > 0):
			return $query->result_array();
		else:
			return false;
		endif;
	}	// END OF FUNCTION

    /* * *********************************************************************
	 * * Function name : sendSms 
	 * * Developed By : Tejaswi
	 * * Purpose  : This function used for send SMS to multiple recipients
	 * * Date : 09 August 2021
	 * * **********************************************************************/
	public function sendSms($phone='',$message='') {
		if(!empty($phone) && !empty($message)):
			curl_setopt_array($curl, array(
				CURLOPT_URL => 'http://domain.in/api/v4/?api_key=Aa6da2d9475600821ff6f1ba9601a8ff6&method=sms&message='.$message.'.&to='.$phone.'&sender=SENDER_NAME',
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_ENCODING => '',
				CURLOPT_MAXREDIRS => 10,
				CURLOPT_TIMEOUT => 0,
				CURLOPT_FOLLOWLOCATION => true,
				CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				CURLOPT_CUSTOMREQUEST => 'GET',
				CURLOPT_HTTPHEADER => 0,
			));

			$response = curl_exec($curl);

			curl_close($curl);
			echo $response;
		endif;
	}
	
	

Here the URL i  passed inside CURLOPT_URL will be different for different SMS gateway so you just need to paste your gateway provided URL and replace the variables $message & $phone with the static text message and static phone number.

This is the way to send SMS to multiple users, if you want to send text message to individual user just change the result_array to row array in the sms_model  for getData function and  remove the foreach loop in the controller.

This way you can send  SMS to multiple numbers using PHP code With CURL.

Let me know your reviews through comments and do not forget to share with your friends & colleagues whom might be useful.

And please subscribe to our notification to get instant updates.To subscribe just click on the allow button once you open our page.

subscribe

Related articles:

 

For more updates:

Follow us on our Facebook page :
Facebook

Do Follow my Instagram page for more updates :
Instagram

Leave a Reply

Your email address will not be published. Required fields are marked *

The maximum upload file size: 2 MB. You can upload: image, document. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded.