Send SMS in PHP

Bulk SMS API Documentation

Send SMS in PHP - Sample Code

The following bulk SMS API sample in PHP can be used to send out text messages in Single, Bulk, Group and Excel Upload.

Through Excel Upload we allow 2 types of text messages to be sent, one is normal with mobile numbers and second one is Custom. In custom, you can upload custom parameters which will be explained in detail in this documentation.

This sample API is for PHP both in POST or GET method.

You can also download PHP SMS Class.

Execute PHP SMS Sample Code - Live Demo

Application API URL:

https://www.smsgateway.center/SMSApi/rest/send

Key Value Description
Login Credentials (Required Parameters)
Authenticate your API request using userId-password or apiKey. You should use either one to authenticate your login.
userId Your Registered Username The registered username parameter to be passed. You can use this if apiKey is not being used.
password Your password The password needs to be urlencoded if there are any special characters used in the password field. You can use this if apiKey is not being used.
apiKey Your unique apiKey apiKey needs to be sent as HTTP header when you are not using userId and password credentials. You can avail this from your user control panel and use instead of userId and password HTTP Request parameter. Please do not disclose this to anyone.
Required Parameters
sendMethod simpleMsg | groupMsg | excelMsg

simpleMsg: This value required for single or comma separated mobile numbers SMS.

groupMsg: This value required for Group SMS option.

excelMsg: This value required for Bulk Upload option.

senderId Approved Sender Name For Indian customers, sender name would be of 6 alpha characters. For International customers, sender name depends on country to country basis. senderId parameter is an optional for Promotional Gateway customers. You need not include this parameter if you have opted for promotional gateway.
msgType text|unicode This is to define simple or UNICODE message. For simple English, you can give value as text or for non-English messages, give value as unicode.
msg Your text message This is the message content which you want to send to your clients.

View Cost/Count
Default Parameters (Optional)
format plain|json|xml Value for response format.

System default is plain. If you need responses in JSON or XML then you have to give value as JSON or XML.
flashMsg true|false Maximum 160 characters allowed in message content. You could send unicode messages too.
System default is false.
duplicateCheck true|false Checks and removes Duplicate mobile numbers.

System default is true. You can turn off duplicate checking of mobile numbers by giving value as false, duplicate numbers wont be removed.
scheduleTime datetime format (YYYY-MM-DD HH:MM:SS) Value for Scheduling.

System default is null. If you want to enable then use this parameter and give value as YYYY-MM-DD HH:MM:SS.
Simple Message - Single or Bulk (Required for simpleMsg)
mobile 10 or 12 Digits mobile number. Mobile number format can be 10 to 12 digits prefix with 0 or 91. Preferred mobile format is prefix with country code 91. You can include single number or comma separated numbers. Maximum comma separated mobile numbers allowed are 1000.

While sending Simple message, sendMethod parameter needs to be simpleMsg
Group Message - Single or Multiple Groups (Required for groupMsg)
group Group IDs / Group Names.
Example:

1123,1321 OR

mygroup, othergroup OR

1123,mygroup
Sending SMS to your saved Contacts grouped by Group Name or Group ID.

You can send to single group or multiple groups at once separated by comma. Valid Group IDs or Group names should be mentioned in this parameter. For multiple groups, please separate by comma.

While sending Group SMS message, sendMethod parameter needs to be groupMsg
Bulk Upload (Required for excelMsg)
file file path File path to be given of the file.

While sending Bulk Upload message, sendMethod parameter needs to be excelMsg
fileType csv | xls | xlsx | zip | txt We allow 5 extensions in bulk upload.

Maximum File Size Allowed: 15MB

DLT (Only for Indian Delivery Network)
dltEntityId Your DLT's Principal Entity ID Optional. This is an optional parameter if you have saved your Principal DLT Entity ID in your profile settings. If this is not saved in profile, then this is a mandatory parameter.
dltTemplateId Your DLT's Approved Message Template ID Optional. This is an optional parameter if you have saved your message templates. If this is not saved in message templates, then this is a mandatory parameter.
Link Tracking
trackLink true | false System default is false. Set this to true if you want to enable advanced link tracking. The last URL from your message will be converted to short URL. If you do not use this parameter, the value of this parameter will be false.
smartLinkTitle string Optional. This acts as identifier for your tracking, You can name your smart link title to check insights based on your title.
Message Encryption
encrypt true | false System default is false. Your account must have been enabled Message Encryption flag. You have to set this to true if you are sending encrypted message over API, so that system will decrypt and send actual message to users. You should have also generated "Message Encryption Secret Token" along with enabling flag..
Test
testMessage true | false System default is false. Set this to true if you want to test our API. Messages wont be delivered and SMS balance wont be deducted. If you do not use this parameter, the value of this parameter will be false.

Save this Page on your Favorite Social Media

PHP SMS Sample Request Code

POST METHOD

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.smsgateway.center/SMSApi/rest/send",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "userId=user&password=userpass&senderId=SMSGAT&sendMethod=simpleMsg&msgType=text&mobile=9199999999999&msg=This%20is%20my%20first%20message%20with%20SMSGateway.Center&duplicateCheck=true&dltEntityId=xxxxxxxxxxxx&dltTemplateId=xxxxxxxxxxxx&format=json&scheduleTime=2017-06-13%2020%3A22%3A00",
  CURLOPT_HTTPHEADER => array(
    "apikey: somerandomuniquekey",
    "cache-control: no-cache",
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

GET METHOD

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.smsgateway.center/SMSApi/rest/send?userId=user&password=userpass&senderId=SMSGAT&sendMethod=simpleMsg&msgType=text&mobile=9199999999999&msg=This%20is%20my%20first%20message%20with%20SMSGateway.Center&duplicateCheck=true&&dltEntityId=xxxxxxxxxxxx&dltTemplateId=xxxxxxxxxxxxformat=json&scheduleTime=2017-06-13%2020%3A22%3A00",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

POST METHOD

<?php

$request = new HttpRequest();
$request->setUrl('https://www.smsgateway.center/SMSApi/rest/send');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/x-www-form-urlencoded',
  'apikey' => 'somerandomuniquekey'
));

$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
  'userId' => 'user',
  'password' => 'userpass',
  'senderId' => 'SMSGAT',
  'sendMethod' => 'simpleMsg',
  'msgType' => 'text',
  'mobile' => '9199999999999',
  'msg' => 'This is my first message with SMSGateway.Center',
  'duplicateCheck' => 'true',
  'dltEntityId' => 'xxxxxxxxxxxx',
  'dltTemplateId' => 'xxxxxxxxxxxx',
  'format' => 'json',
  'scheduleTime' => '2017-06-13 20:22:00'
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

GET METHOD

<?php

$request = new HttpRequest();
$request->setUrl('https://www.smsgateway.center/SMSApi/rest/send');
$request->setMethod(HTTP_METH_GET);

$request->setQueryData(array(
  'userId' => 'user',
  'password' => 'userpass',
  'senderId' => 'SMSGAT',
  'sendMethod' => 'simpleMsg',
  'msgType' => 'text',
  'mobile' => '9199999999999',
  'msg' => 'This is my first message with SMSGateway.Center',
  'duplicateCheck' => 'true',
  'dltEntityId' => 'xxxxxxxxxxxx',
  'dltTemplateId' => 'xxxxxxxxxxxx',
  'format' => 'json',
  'scheduleTime' => '2017-06-13 20:22:00'
));

$request->setHeaders(array(
  'cache-control' => 'no-cache'
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

POST METHOD

<?php

$client = new http\Client;
$request = new http\Client\Request;

$body = new http\Message\Body;
$body->append(new http\QueryString(array(
  'userId' => 'user',
  'password' => 'userpass',
  'senderId' => 'SMSGAT',
  'sendMethod' => 'simpleMsg',
  'msgType' => 'text',
  'mobile' => '9199999999999',
  'msg' => 'This is my first message with SMSGateway.Center',
  'duplicateCheck' => 'true',
  'dltEntityId' => 'xxxxxxxxxxxx',
  'dltTemplateId' => 'xxxxxxxxxxxx',
  'format' => 'json',
  'scheduleTime' => '2017-06-13 20:22:00'
)));

$request->setRequestUrl('https://www.smsgateway.center/SMSApi/rest/send');
$request->setRequestMethod('POST');
$request->setBody($body);

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/x-www-form-urlencoded',
  'apikey' => 'somerandomuniquekey'
));

$client->enqueue($request)->send();
$response = $client->getResponse();

echo $response->getBody();

GET METHOD

<?php

$client = new http\Client;
$request = new http\Client\Request;

$request->setRequestUrl('https://www.smsgateway.center/SMSApi/rest/send');
$request->setRequestMethod('GET');
$request->setQuery(new http\QueryString(array(
  'userId' => 'user',
  'password' => 'userpass',
  'senderId' => 'SMSGAT',
  'sendMethod' => 'simpleMsg',
  'msgType' => 'text',
  'mobile' => '9199999999999',
  'msg' => 'This is my first message with SMSGateway.Center',
  'duplicateCheck' => 'true',
  'dltEntityId' => 'xxxxxxxxxxxx',
  'dltTemplateId' => 'xxxxxxxxxxxx',
  'format' => 'json',
  'scheduleTime' => '2017-06-13 20:22:00'
)));

$request->setHeaders(array(
  'cache-control' => 'no-cache'
));

$client->enqueue($request)->send();
$response = $client->getResponse();

echo $response->getBody();

Response

{
    "status": "success",
    "mobile": "919999999999, 919999999998",
    "invalidMobile": "",
    "transactionId": "595748da-bab8-4da7-ae9e-5a97007ae49b",
    "statusCode": "900",
    "reason": "success"
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<simpleMessageSuccessResponse>
    <invalidMobile></invalidMobile>
    <mobile>919999999999, 919999999998</mobile>
    <reason>success</reason>
    <status>success</status>
    <statusCode>900</statusCode>
    <transactionId>608e6f4a-5ab6-49a2-b2ea-02e47005d794</transactionId>
</simpleMessageSuccessResponse>
status=success | mobile=919999999999, 919999999998 | invalidMobile= | transactionId=b4c7a895-773e-4cff-b4cb-5df2d6fa4139 | errorCode=900 | reason=success