SMS Gateway API Integration in PHP - Sending Text Messages

On 9/11/2017

Hi! In this tutorial, we'll look at how to integrate sms api in php and send bulk text messages using simple php script. These days, increasing number of web applications uses SMS feature for product promotions, user authentication or sales and event notifications directly through their mobile phones. To send sms via php, you need to integrate an appropriate SMS gateway provider in your application. There are a number of SMS Service providers in the market and they offer APIs in various programming languages.

Here I'm going to show you how to integrate Textlocal SMS gateway in PHP.

sms gateway integration in php

Read: How to Create AJAX Search Engine using PHP and MySQL

Textlocal SMS API:

Textlocal is a bulk sms gateway that offers an easy and powerful messaging platform. It helps you maintain massive contact lists, compose rich multi-media messages and send sms in bulk. It has flexible APIs supporting different programming platforms.

Register with SMS Gateway:

In order to use Textlocal sms gateway in your web app, you must first register with their service and get your hash code.

Visit this link and signup for the sms service. It is necessary to provide details such as email address, mobile number and password for registration. At the beginning you will get 10 free sms credits which you can use for testing.

Read: User Login and Registration System using PHP and MySQL

Once you finish signing up, login and go to the Dashboard.

From there, navigate to 'Help' > 'All Documentation' page and get your API hash code.

You also need to download the API library textlocal.class.php. For that go to http://api.textlocal.in/docs/phpclass and click on 'Download' link on the righ side of the page.

SMS API Integration in PHP:

Extract and move 'textlocal.class.php' to your root folder. You have to include this class in your php code and use the sendSms() function to send text messages. To test the service, you can send sms from localhost. Here's the php code to do it.

<?php
require('textlocal.class.php');

$username = 'myemail@example.com'; // change this to your email address
$hashcode = 'xxxxxxx'; // change this to yours

$textlocal = new Textlocal($username, $hashcode);
$numbers = array(919999999999); // separate multiple numbers by comma
$sender = 'TXTLCL';
$message = 'Hi! This is a test message!!!';

try {
    $result = $textlocal->sendSms($numbers, $message, $sender);
    $data = json_decode($result, true);
    if($data['status'] == 'success'){
        echo 'SMS sent successfully!';
    }
} catch(Exception $e) {
    die('Error: ' . $e->getMessage());
}
?>

To send sms in bulk, use comma-delimited array list of mobile numbers. Also make sure the numbers are in international format i.e. country code followed by 10-digit mobile no. A maximum of 10,000 numbers are allowed at a time.

Run the above code and if all goes well, you will see success message. I received the sms within couple of seconds. Remember that you will only get ten sms credits under free account. So use it wisely to test the code and not run it in a loop.

Read: How to Import JSON into MySQL using PHP

By default, the Textlocal API will send the response as JSON. This is the response I received from the API.

{
    "balance": 8,
    "batch_id": 302329336,
    "cost": 1,
    "num_messages": 1,
    "message": {
        "num_parts": 1,
        "sender": "TXTLCL",
        "content": "Hi! This is a test message!!!"
    },
    "receipt_url": "",
    "custom": "",
    "messages": [
        {
            "id": "1200053892",
            "recipient": 919999999999
        }
    ],
    "status": "success"
}

The API returns a status field containing the message 'Success' or 'Failure'. You can use it to check if sms is sent or not.

You can also display the entire api response this way,

<?php
$result = $textlocal->sendSms($numbers, $message, $sender);
print_r($result);
?>

Read: Store and Retrieve Image from MySQL Database using PHP

Likewise you can send bulk sms in php via the Textlocal sms gateway. The entire process is simple and with few lines of code you can send bulk messages from your website. You can also use the cURL method and send an HTTP request to the sms server. I hope you like this tutorial. Please don't forget to share it in social media.

No comments:

Post a Comment

Contact Form

Name

Email *

Message *