Why Send SMS Programmatically?
Automating SMS sending through code (programmatically) allows your application to trigger SMS notifications automatically — order confirmations, appointment reminders, verification codes, marketing campaigns, and more — without manual intervention.
Prerequisites
- PHP 7.4 or higher with cURL extension enabled
- API credentials from Digital Services Center
- A basic understanding of PHP and HTTP requests
Basic PHP Function
<?php
function sendBulkSMS($api_key, $username, $password, $recipients, $message, $dlrurl = '') {
$url = 'https://bulksms.schooldream.co.rw/api/v2/sms/send';
$data = [
'api_key' => $api_key,
'username' => $username,
'password' => $password,
'recipients' => is_array($recipients) ? implode(',', $recipients) : $recipients,
'message' => $message,
];
if ($dlrurl) $data['dlrurl'] = $dlrurl;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return ['status' => 'error', 'message' => $error];
}
return json_decode($response, true);
}
// Example usage:
$result = sendBulkSMS(
'your_api_key',
'your_username',
'your_password',
['250782589800', '250782589801'],
'Your order #1234 has been shipped!',
'https://yourdomain.com/dlr-handler'
);
if ($result['status'] === 'success') {
echo "Sent " . $result['totalmessages'] . " messages!";
} else {
echo "Error: " . $result['message'];
}
?>
Error Handling
Always check the response status and handle errors appropriately. Common error codes include invalid credentials (401), daily limit reached (429), and invalid phone numbers (400).
Best Practices
- Store API credentials in environment variables, not in source code
- Implement retry logic for transient failures
- Log all API requests and responses for debugging
- Use the dlrurl parameter to track delivery status asynchronously
- Test thoroughly with free testing mode (15 SMS/day) before going live
Full API Documentation → · Interactive Playground →