How to integrate sms api in codeigniter using Spring Edge sms gateway

integrate sms api in codeigniter

Below is a detailed walk through Integration of SMS gateway notifications API to php Codeigniter.

Codeigniter is a lightweight MVC based framework to develop web-based applications. Codeigniter is an open-source framework built in PHP language. Using codeigniter Developers can easily build large web software or a small website. Codeigniter has a good community and documentation, Its simple yet powerful framework which help developers to minimize code and maintain coding standard to build robust dynamic web-based applications.

Codeigniter libraries, plugins and helpers make easier to write and maintain source code. It is easy to add new functionality like integrating sms is easy to add using codeigniter helpers.

This article is about how to integrate sms gateway functionality with php codeigniter using spring edge sms gateway APIs.

Requirements:

  • knowledge of PHP and Codeigniter.
  • Account/API key with Spring Edge SMS Gateway.
  • SMS Sender Name provided by Spring Edge.
  • A Working environment to run codeigniter.

Lets get started:

Sign Up With Spring Edge SMS Gateway:

Fill an online form with your name, email and other details on www.springedge.com website to sign up. Upon sign up for sms account, you will get trail credits in account to get started with. You can use these sms credits by sending sms using spring edge web app or sms API.

Generating API Auth Key:

Auth Key is used to secure access of our sms account and prevent unauthorised access. Auth key is mandatory to send sms using RestAPIs. You can generate auth key using “Developers” menu of sms application. For security purpose Auth keys are communicated to registered email address only.

Generating API Auth Key

Send a test message:

You can send test sms by two ways, user-interface and RestAPI. You can login into your sms account and send sms using Compose Message menu. You can check sent sms report for acknowledgement of recently sent messages.

Now, Integrate with codeigniter

To start with codeigniter sms gateway integration, we will create a helper in codeigniter helpers folder. Helper functions can be called from any controller in codeigniter.

Create a new file application/helpers/sendsms_helper.php with below content:

<?php
function sendsms($mobileno, $message){

    $message = urlencode($message);
    $sender = 'SEDEMO'; 
    $apikey = 'API_KEY_HERE';
    $baseurl = 'https://instantalerts.co/api/web/send?apikey='.$apikey;

    $url = $baseurl.'&sender='.$sender.'&to='.$mobileno.'&message='.$message;    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    // Use file get contents when CURL is not installed on server.
    if(!$response){
        $response = file_get_contents($url);
    } 
}
?>

In helper function we have used CURL to call sms api. It is recommanded to make sure CURL is installed on server. We have also used file get contents function as an alternative.

Load sms helper in controller:

To send sms from codeigniter controller, we have to load helper first. We can use below code to load send sms helper. This code can be placed in constructor.

$this->load->helper('sendsms_helper');

Call sendsms function:

After helper loaded, its function will be available directly. You can just use below code to call send text message to given mobile number.

sendsms( '919918xxxxxx', 'Hi, this is a test message' );

This code will call the helper function which will trigger sms api and deliver sms to mobile number.

Deployment on production:

To configure it in a production environment of your codeigniter application, You need to move from existing Test/Trail sms gateway account to a commercial. Once commercial account is set-up, You will need to configure auth key and customized sender name in helper.

Conclusion:

To integrate sms gateway functionality with codeigniter, we have created a helper function which take mobile number and text message body as input. We have used RestAPI from spring edge sms gateway to send sms.


	

Leave a Reply

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