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($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$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($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$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
Hello,
The script is good enough. But If I want to send dynamic messages to any dynamic number after any form submission where I have added/integrated sms api key.
Your code contains ‘receipientno’ => ‘XXXXXXXXXX’, so here I want to call dynamic php contact number id, so how should I call there in your script??
Thanks a lot, though I required some changes to be done, but it worked fine…
nice
Thank You for this valuable information and it is very crisp and clear.
I am using this and it is working fine.
I am facing one issue, I think it is because of SMS service provider. The response is taking ~10 Secs.
I am using above code from my controller when business flow is valid.
Do you see any possibility that we invoke above request but let’s not wait for response. I mean send SMS request to Service Provider and continue with further steps. (At least in this scenario, I am not interested to know whether my SMS request is success or not but I want my business flow should be executed little faster and should not hinder because of delay from SMS service provider)