Send SMS via PHP using SMSGatewayHub

We have all received text messages from banks, eCommerce websites and even Digital TV providers regarding bill payment remainders, new features and updates on your service. These and quite useful (although sometimes annoying) as they provide updates about services/products or information about new ones.

To send SMS messages from your website or app, you will need to enlist the services of a third party. This third party SMS service is provided by a lot of different companies. Here we use one such company SMSGatewayHub to send SMS messages. I have personally tried their services and the journey so far has been smooth.

SmsGatewayHub Logo

There are other companies of course, for example MSG91 which provides startups with 10,000 SMS messages free every month provided your add their logo onto your website. If you’re short of funds MSG91 seems like a viable option.

Part 1: Setting up your SMSGatewayHub Account

Getting Started

Be we use PHP to send SMS messages to clients, we need to set up an account at SMSGatewayHub. So to begin, create an account with SMSGatewayHub. This should provide you 20 free messages. Try them out.

Once (and if) you have decided that you’re okay with their services you can get started.

Registering Your SenderId

Your SenderId is what the recipients of your SMS messages see instead of a phone number. For example Flipkart has TD-FLPKRT, State Bank Of India has BX-ATMSBI, BX-SBIINB among others. You get to decide what name appears on messages from your company.

To register your SenderId, login and head over to your dashboard.

From the left menu select: Text SMS ->Templates -> My SenderIds. Select ‘Add New SenderId’. This gives you the following pop up

Registering Sender ID

Enter your desired SenderId and the purpose of the SenderId.

Promotional vs Transactional SMS:

There are two types of SMS messages that you can send. Select the one which suits you best.

Promotional SMS : These are usually messages about new features and offers which are sent to all customers. They are cheaper than transactional SMS messages. Eg: Flikpart sending messages to all customers about its Big Billion Day sale.

Transactional SMS: These are sent to individual customers, and follow the predefined templates. Eg: Messages you get from Flipkart informing you that your purchased item has been dispatched/shipped.

As mentioned in the pop up, drop an email to once you have saved you request. The approval usually happens within two business days.

SMS Templates:

If you need only Promotional SMS services, you can skip this section. But if you need to send Transactional SMS messages, there you need to complete one more step.

Before sending transactional messages, you need to register at least one SMS template. A template is an outline on which your messages will be based around. Here are a few sample templates provided as examples

  • Hello %customer_name%! You order has been received and will be delivered before %delivery_date%.
  • Your cancellation request with number %ticket_number% has been received. We will get back to you soon.
  • Your OPT for the transaction is %OTP%.

In all the templates you would have notices some content between a double percentage (eg: %customer_name%). This is the variable part of your message, i.e the part that changes with every message. The rest is constant.For example, the first template would become:

  • Hello Adarsh! You order has been received and will be delivered before 30 April.

You can create custom templates and you can use them after they are approved by SMSGatewayHub.

To create a template, from the left menu select: Text SMS ->Templates -> My Templates. Select ‘Add New Template’. This gives you the following pop up

Registering A Transactional Template

Type in the name of your template and the content and save it. Like with the SenderId registration, send an email to just to be sure.

Generating an API Key:

When making API calls from your PHP application, you will need an API key to authenticate yourself. Your API key is unique to your account and should not be revealed to others.

To generate an API key from the left menu select Api Documents -> Api Codes and click on ‘Save’ beside the random string that you see. This random string is your API Key. Copy it, and keep it aside.

Once both your SenderId and and SMS template(s) (only for transactional messages) have been approved, you can begin with part 2.

Part 2: Using PHP to send your SMS

To send SMS messages from you application, we need to make API calls to SMSGatewayHub.

The request is made to the URL:

To this URL, we append a few parameters. These parameters (case sensitive) are:

  • APIKey: The API Key that you previously generated.
  • senderid: Approved SenderId.
  • channel: Promotional=1 or Transactional=2.
  • route: Usually 1 indicating a normal route.
  • DCS: 1 for regular messages, 8 for Unicode messages (containing specials character/ Indian language characters).
  • flashsms: Flash messages are immediately displayed and are not saved by default (Default is 0 for normal SMS, Set 1 for immediate display).
  • number: The phone number(s) of the recipient(s). If there is more than one phone number use a ,(comma) to separate them (Only 100 mobile numbers are allowed).
  • text: The actual message content.

Option Parameter:

  • schedtime: If this parameter is not used, then messages are sent immediately. If this parameter is set the message is sent at the specified time. The format is yyyy/mm/dd hh:mm:ss PM/AM


The PHP Class:

We write a simple PHP class to send our messages.

We define the following class variables:

In the class constructor, we assign values to variables.

The template class variable is an associative array of approved template names for keys and the actual templates as values. The only difference being that all variable components are replaced with %s.

Note: For a line break, use \n

The default values of the parameters that we have defined can be changed dynamically using the setParameter() method.

Setting Recipient Phone Numbers:

To set phone numbers, we define a method setNumbers() which takes an array of phone numbers. The method simply joins all the numbers together into a string using PHP’s implode function.

Setting the SMS content:

If we are sending transactional messages, the content of the message should be a template with the variable part changed. To do this, we use PHP’s vsprintf() method. vsprintf() takes two parameters, the string to be formatted and an array containing values to be inserted in the string.

For promotional messages, the procedure is simple. The ‘text’ parameter is set to the content of the message.

The complete script can be found at Github.

Sending the SMS:

Before sending messages, ensure that you have downloaded the script and inserted your APIKey and SenderId.

Promotional SMS:

Transactional SMS:


Although it seems like an overly complicated process, in actuality its quite simple.


Howdy! You're reading my blog. To know more about me click here

Leave a Reply

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