Prerequisites
Integrating SMS capability into your PHP application is easier than you think. You will need:
- An account with OnlineSMSService (Get API Key)
- PHP 7.4 or higher installed
- cURL extension enabled
Step 1: Get Your API Key
Login to your dashboard and navigate to Settings > API Keys. Copy your API Key.
Step 2: The PHP Code
Here is a robust function to send SMS using cURL:
function sendSMS($numbers, $message) {
$apiKey = "YOUR_API_KEY";
$senderId = "YOUR_SENDER_ID"; // e.g., ONLSMS
// Prepare data
$data = [
"apikey" => $apiKey,
"sender" => $senderId,
"numbers" => $numbers,
"message" => $message
];
// Initialize cURL
$ch = curl_init("https://api.onlinesmsservice.com/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Usage
$result = sendSMS("919876543210", "Your OTP is 123456");
print_r($result);
Step 3: Handle the Response
The API will return a JSON response indicating success or failure. Always check the `status` field.
{
"status": "success",
"message_id": "MSG123456789",
"cost": 0.10
}
Tips for Production
- Always run SMS calls asynchronously (using Queues) to avoid blocking your page load.
- Log all API responses for debugging.
- Monitor your credit balance using the Balance API.
Need help? Our developer support team is available 24/7.