💬 WhatsApp API @ Best Rate | Zero Setup Fee → 99.9% Uptime OTP Delivery — Try FREE Demo! 🏆 10,000+ Registered Businesses — 2,000+ Enterprise Clients Served 🚀 API Integration in Under 5 Minutes 🏅 Verified Business Badge with RCS Messaging 💬 WhatsApp API @ Best Rate | Zero Setup Fee → 99.9% Uptime OTP Delivery — Try FREE Demo! 🏆 10,000+ Registered Businesses — 2,000+ Enterprise Clients Served 🚀 API Integration in Under 5 Minutes 🏅 Verified Business Badge with RCS Messaging
Talk to Sales: India 1800 102 1822
Bulk SMS

Sample Code for Sending SMS in PHP – Complete Guide

182 views

Why Use PHP to Send SMS via an API ?

PHP remains one of the most widely used server-side programming languages in the world, powering millions of websites, web applications, and enterprise platforms. When businesses need to send transactional alerts, OTPs, promotional messages, or bulk notifications, integrating an SMS API with PHP is one of the most practical and efficient solutions available.

Using a reliable SMS gateway API like SMSGatewayHub makes it straightforward to send SMS messages from any PHP application. Whether you are building a login OTP system, an e-commerce order alert, or a bulk notification platform, PHP's flexibility and HTTP request capabilities make integration seamless.

Prerequisites Before You Start

Before diving into the PHP sample code, ensure you have the following ready:

  • An active SMSGatewayHub account – Sign up and get your API key from the dashboard.
  • API credentials – Your API key, sender ID, and approved DLT template (mandatory for Indian SMS traffic).
  • PHP version 5.6 or higher – The code works with both older and modern PHP versions.
  • cURL enabled – Most hosting environments have cURL enabled by default. Confirm with phpinfo().
  • A registered DLT sender ID – Required for sending SMS in India as per TRAI regulations.

Understanding the SMSGatewayHub SMS API

SMSGatewayHub's Bulk SMS API supports RESTful HTTP-based requests, making it easy to integrate with PHP using cURL or file_get_contents. The API accepts GET or POST requests and returns a JSON or text-based response confirming whether the message was submitted successfully.

The key API parameters include your authentication token, the recipient mobile number, the message body, the sender ID, and the DLT template ID. Let's walk through the actual PHP implementation below.

PHP Sample Code to Send SMS Using cURL

The following example demonstrates how to send a single SMS using PHP and cURL with the SMSGatewayHub API:

<?php // SMSGatewayHub - Send SMS via PHP using cURL $apiKey = 'YOUR_API_KEY'; $senderId = 'SMSHUB'; $mobile = '9XXXXXXXXXX'; $message = urlencode('Your OTP is 123456. Valid for 10 minutes.'); $templateId = 'YOUR_DLT_TEMPLATE_ID'; $url = "https://www.smsgatewayhub.com/api/mt/SendSMS?APIKey={$apiKey}&senderid={$senderId}&channel=Trans&DCS=0&flashsms=0&number={$mobile}&text={$message}&DLTTemplateId={$templateId}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); curl_close($ch); echo $response; ?>

Replace YOUR_API_KEY, SMSHUB, and YOUR_DLT_TEMPLATE_ID with the actual values from your SMSGatewayHub account. This basic script initiates an HTTP GET request to the API endpoint and prints the server's response.

PHP Sample Code to Send Bulk SMS

For sending messages to multiple recipients at once, you can loop through an array of mobile numbers:

<?php $apiKey = 'YOUR_API_KEY'; $senderId = 'SMSHUB'; $message = urlencode('Hello! Your order has been shipped. Track it on our website.'); $templateId = 'YOUR_DLT_TEMPLATE_ID'; $mobileNumbers = ['9XXXXXXXXXX', '8XXXXXXXXXX', '7XXXXXXXXXX']; foreach ($mobileNumbers as $mobile) {    $url = "https://www.smsgatewayhub.com/api/mt/SendSMS?APIKey={$apiKey}&senderid={$senderId}&channel=Trans&DCS=0&flashsms=0&number={$mobile}&text={$message}&DLTTemplateId={$templateId}";    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    $response = curl_exec($ch);    curl_close($ch);    echo "Sent to {$mobile}: " . $response . "<br>"; } ?>

This loop-based approach is ideal for small to medium batch sends. For enterprise-level bulk SMS campaigns, use the API's batch number parameter to pass comma-separated numbers in a single API call, which improves performance significantly.

Sending OTP SMS with PHP

OTP (One-Time Password) delivery is one of the most common use cases for PHP SMS integration. Using SMSGatewayHub's OTP SMS service, you can send time-sensitive codes with high deliverability and low latency. Simply set the channel parameter to Trans (Transactional) and ensure your DLT template is approved for OTP messaging.

For added security, always generate OTPs server-side, store them with an expiry timestamp, and validate the user's input before granting access.

Handling API Responses in PHP

SMSGatewayHub's API returns a structured response you can parse for success or error handling:

  • Success – Response contains a message ID confirming submission.
  • Invalid API Key – Check your credentials in the dashboard.
  • DLT Template Mismatch – Ensure the message body matches your registered template exactly.
  • Insufficient Credits – Recharge your SMSGatewayHub wallet to continue sending.

Use PHP's json_decode() function if the API returns JSON to programmatically handle each status.

Best Practices for PHP SMS Integration

  • Always URL-encode the message text using urlencode() to handle special characters.
  • Store your API key in environment variables or a config file — never hardcode it in public scripts.
  • Implement retry logic for failed API calls to improve delivery reliability.
  • Log all API responses for debugging and audit purposes.
  • Comply with TRAI DLT regulations by registering your sender ID and templates before sending commercial SMS in India.

Conclusion

Integrating SMS functionality into your PHP application is straightforward when you use a reliable and developer-friendly gateway like SMSGatewayHub. The sample code provided above gives you a working foundation for sending transactional SMS, OTPs, and bulk notifications directly from your PHP scripts.

With features like high deliverability, DLT compliance, real-time delivery reports, and competitive pricing, SMSGatewayHub is the ideal SMS API partner for developers and businesses in India. Sign up today, grab your API key, and start sending SMS in PHP within minutes.