Installation:
composer require dreamsms/laravel-dreamsms
php artisan vendor:publish --provider="DreamSms\LaravelDreamSms\DreamSmsServiceProvider" --tag=config
Add credentials to .env:
DREAMSMS_BASE_URL=https://www.dreams.sa/index.php/api
DREAMSMS_USER=your_username
DREAMSMS_SECRET_KEY=your_api_key
DREAMSMS_SENDER_NAME=YourSenderID
First Use Case: Send a test SMS via Tinker:
use DreamSms\LaravelDreamSms\Facades\DreamSms;
DreamSms::send('+966123456789', 'Hello from Laravel!');
DreamSms (global access)app(DreamSms::class)config('dreamsms')Sending SMS
DreamSms::send('+966123456789', 'Your OTP is 1234');
$recipients = ['+966123456789', '+966987654321'];
DreamSms::sendMultiple($recipients, 'Hello all!');
OTP Management
$otp = DreamSms::generateOtp();
DreamSms::sendOtp('+966123456789', $otp);
Balance & Account
$balance = DreamSms::getBalance();
use DreamSms\Jobs\SendSmsJob;
SendSmsJob::dispatch('+966123456789', 'Delayed message');
SmsSent):
DreamSms::send('+966123456789', 'Event-triggered');
DreamSms::setSender('CustomSender')->send('+966123456789', 'Message');
Authentication Failures
DREAMSMS_SECRET_KEY matches the API key from your DreamSMS dashboard.DREAMSMS_BASE_URL is correct (some accounts use https://api.dreams.sa).Sender Name Restrictions
YourBrand not yourbrand).DreamSMS) before submitting for approval.Rate Limits
429 Too Many Requests.try {
DreamSms::send($number, $message);
} catch (\DreamSms\Exceptions\RateLimitException $e) {
sleep(2); // Wait before retry
retry();
}
OTP Length
$otp = str_pad(rand(0, 999999), 6, '0', STR_PAD_LEFT);
Enable Logging:
Add to config/dreamsms.php:
'debug' => env('APP_DEBUG', false),
Logs appear in storage/logs/laravel.log.
API Response Inspection:
Use the getLastResponse() method to debug raw API calls:
DreamSms::send('+966123456789', 'Test');
dd(DreamSms::getLastResponse());
Custom HTTP Client Override the default Guzzle client in a service provider:
$this->app->singleton(DreamSms::class, function ($app) {
$client = new \GuzzleHttp\Client(['timeout' => 30]);
return new \DreamSms\LaravelDreamSms\DreamSms($client, $app['config']['dreamsms']);
});
Event Customization
Extend the SmsSent event to add metadata:
DreamSms::extend(function ($sms) {
$sms->onSent(function ($event) {
\Log::info('Custom event', ['message' => $event->message]);
});
});
Fallback for Failures Implement a fallback sender (e.g., Twilio) if DreamSMS fails:
try {
DreamSms::send($number, $message);
} catch (\Exception $e) {
\Log::error('DreamSMS failed: ' . $e->getMessage());
// Fallback to Twilio or email
}
How can I help you explore Laravel packages today?