PHP SMS API Integration
Complete PHP code examples for integrating with the SecureSMSC SMS API using cURL.
Fast Integration
Get started in minutes with simple REST API calls
Global Reach
Send SMS to 200+ countries worldwide
Secure
Enterprise-grade SSL/TLS encryption
Reliable
99.9% uptime with delivery tracking
Quick Start: Get your API key from the dashboard to begin integration.
Installation
Install the required dependencies:
Terminal
# cURL is built into PHP
Send Single SMS
PHP
<?php
$apiKey = 'YOUR_API_KEY';
$apiUrl = 'https://whitelabled.securesmsc.com/api/v2/sms/send';
function sendSMS($recipient, $message) {
global $apiKey, $apiUrl;
$data = [
'sender_id' => 'SENDER',
'recipient' => $recipient,
'message' => $message,
'route' => 'transactional'
];
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($response, true);
if ($httpCode === 200) {
echo "Success: " . $result['message'] . "\n";
return $result;
} else {
echo "Error: " . $response . "\n";
return null;
}
}
// Usage
sendSMS('1234567890', 'Hello from PHP!');
Send Bulk SMS
PHP
function sendBulkSMS($recipients, $message) {
global $apiKey;
$data = [
'sender_id' => 'SENDER',
'recipients' => $recipients,
'message' => $message,
'route' => 'promotional'
];
$ch = curl_init('https://whitelabled.securesmsc.com/api/v2/sms/send-bulk');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Usage
sendBulkSMS(['1234567890', '0987654321'], 'Bulk message!');
Check SMS Status
PHP
function checkStatus($messageId) {
global $apiKey;
$ch = curl_init("https://whitelabled.securesmsc.com/api/v2/sms/status/{$messageId}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey
]);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
echo "Status: " . $result['data']['status'] . "\n";
return $result;
}
Check Balance
PHP
function getBalance() {
global $apiKey;
$ch = curl_init('https://whitelabled.securesmsc.com/api/v2/balance');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey
]);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
echo "Balance: " . $result['data']['balance'] . " credits\n";
return $result;
}
Error Handling - Laravel
PHP - Laravel
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class SmsService
{
protected $apiKey;
protected $baseUrl;
public function __construct()
{
$this->apiKey = config('services.sms.key');
$this->baseUrl = 'https://whitelabled.securesmsc.com/api/v2';
}
public function send(string $recipient, string $message, int $retries = 3)
{
$attempt = 0;
while ($attempt < $retries) {
try {
$response = Http::withToken($this->apiKey)
->timeout(10)
->post("{$this->baseUrl}/sms/send", [
'sender_id' => config('services.sms.sender_id', 'SENDER'),
'recipient' => $recipient,
'message' => $message,
'route' => 'transactional'
]);
if ($response->successful()) {
return $response->json();
}
throw new \Exception($response->body());
} catch (\Exception $e) {
$attempt++;
if ($attempt >= $retries) {
throw $e;
}
sleep(pow(2, $attempt));
}
}
}
}
// Usage in Controller
use App\Services\SmsService;
class NotificationController extends Controller
{
public function sendSms(Request $request, SmsService $sms)
{
$result = $sms->send($request->phone, $request->message);
return response()->json($result);
}
}