SMS Gateway Integration in PHP

These days SMS feature is used for various purposes in the web application. For example user authentication, OTP verification, and sending notifications to users. To send SMS from a PHP script you need to select the best SMS gateway provider that is suitable for your requirement. Once the SMS gateway and plan selection are completed, now it’s time to integrate SMS gateway in PHP.

In this tutorial, we will show you how to integrate SMS gateway API in PHP. The SMS gateway integration process is very simple and less time is required. Using our example code you can easily send SMS from your website using SMS gateway API and PHP.

Usually, the SMS provider provides 3 types of plans, One-way messages, Two-way messages, and Both-ways messages. One-way messaging allows you to send SMS to the customers, but Two-way messaging not only send SMS but also receive reply from the customer.

Generally, an SMS gateway provides a callback URL for passing some parameters, like API Key, sender number, receiver number, message content, etc. The parameters can differ for the different SMS gateway, based on that you need to change or add parameters in the following script.

SMS Gateway API with GET Request:

// Request parameters array 
$requestParams = array(
    
'apiKey' => 'YOUR_API_KEY',
    
'senderID' => 'CODEXW',
    
'receipientno' => 'XXXXXXXXXXXX',
    
'message' => 'Insert sms content'
);

// Append parameters to API URL
$apiURL "https://api.example.com/sendsms?";
foreach(
$requestParams as $key => $val){
    
$apiURL .= $key.'='.urlencode($val).'&';
}
$apiURL rtrim($apiURL"&");

// Send the GET request with cURL
$ch curl_init();
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$response curl_exec($ch);
curl_close($ch);

// Process API response here
echo $response;

SMS Gateway API with POST Request:

// API URL 
$apiURL "https://api.example.com/sendsms";
  
// Request parameters array
$requestParams = array(
    
'apiKey' => 'YOUR_API_KEY',
    
'senderID' => 'CODEXW',
    
'receipientno' => 'XXXXXXXXXXXX',
    
'message' => 'Insert sms content'
);

// Send the POST request with cURL
$ch curl_init();
curl_setopt($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS$requestParams);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$response curl_exec($ch);
curl_close($ch);

// Process API response here
echo $response;

Note that: The above SMS Gateway Integration script uses the cURL method, make sure that the cURL is enabled in PHP.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

4 Comments

  1. Tejas Said...
  2. Himanshu Goel Said...
  3. Rizwan Khan A Said...
  4. Bhushan Said...

Leave a reply

keyboard_double_arrow_up