Skip to main content

SMS Gateway Integration in PHP

These days SMS feature is used for the various purpose in the web application. For example user authentication, OTP verification, sending the notification to users. To send SMS from PHP script you need to select a best SMS gateway provider that suitable for your requirement. Once the SMS gateway and plan selection are completed, now it’s time to integrate SMS gateway in PHP script.
In this tutorial, we will show you how to integrate SMS gateway API in PHP. SMS gateway integration process is very simple and less time 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 plan, One-way messages, Two-way messages, and Both-ways messages. One-way messaging allow you to send SMS to the customers, but Two-way messaging allow not only the send SMS but also receive a reply from the customer.
Generally, 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.

//request parameters array
$requestParams = array(
    'user' => 'codexworld',
    'apiKey' => 'dssf645fddfgh565',
    'senderID' => 'CODEXW',
    'receipientno' => 'XXXXXXXXXX',
    'message' => 'Insert sms content'
);

//merge API url and parameters
$apiUrl = "http://api.example.com/http/sendsms?";
foreach($requestParams as $key => $val){
    $apiUrl .= $key.'='.urlencode($val).'&';
}
$apiUrl = rtrim($apiUrl, "&");

//API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_exec($ch);
curl_close($ch);
Note that: The above SMS Gateway Integration script uses cURL, make sure that the cURL is enabled in PHP.