SMS Send using CodeIgniter

Following Detail Learn, how to integrate sms api in CI. Codeigniter libraries, plugins and helpers make easy to use add new functionality like include SMS.

Requirements:

  1. Account/API key with Spring Edge SMS Gateway.
  2. SMS Sender Name provided by Spring Edge.
  3. A Working environment to run codeigniter.

1 step)

Sign Up With Spring Edge SMS Gateway:

sign up to (www.springedge.com) 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.

2 step)

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.

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.

3 step)

Example :

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

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

    $message = urlencode($message);
    $sender = 'SEDEMO'; 
    $apikey = 'YOUR_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);
    } 
}
?>

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( '919999xxxxxx', 'Hello world, this is a test message' );

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

Leave a Reply

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