h-o-sein/kavenegar-laravel
Laravel integration for the Kavenegar SMS REST API. Install via Composer, configure your API key, and send SMS messages through the Kavenegar PHP client with basic error handling for API and HTTP exceptions.
Installation
composer require h-o-sein/kavenegar-laravel
Ensure your composer.json includes the dependency (though the package appears to be a wrapper for kavenegar/php, not a standalone package).
Publish Config Publish the package config to customize API settings:
php artisan vendor:publish --provider="Hosein\Kavenegar\KavenegarServiceProvider"
Edit .env or config/kavenegar.php with your API key and default sender:
KAVENEGAR_API_KEY=your_api_key_here
KAVENEGAR_SENDER=10004346
First Use Case: Send an SMS Use the facade in a controller or service:
use Hosein\Kavenegar\Facades\Kavenegar;
$result = Kavenegar::send('09123456789', 'Your verification code is 12345');
Or with multiple recipients:
$result = Kavenegar::send(['09123456789', '09367891011'], 'Hello!');
Sending SMS
Kavenegar::send('09123456789', 'Hello!');
Kavenegar::send(['09123456789', '09367891011'], 'Broadcast message');
Kavenegar::send('09123456789', 'Hello!', ['sender' => '10001234']);
Verification Codes
Integrate with Laravel's Illuminate\Auth\Events\Registered or custom events:
use Hosein\Kavenegar\Facades\Kavenegar;
event(new Registered($user));
Kavenegar::send($user->phone, "Your code: {$user->verification_code}");
Async Processing Use Laravel Queues to avoid blocking requests:
use Hosein\Kavenegar\Facades\Kavenegar;
use Illuminate\Support\Facades\Queue;
Queue::push(function () {
Kavenegar::send('09123456789', 'Delayed message');
});
Response Handling Parse responses in middleware or services:
$result = Kavenegar::send('09123456789', 'Test');
if ($result->status === 'success') {
// Success logic
} else {
Log::error('Kavenegar error: ' . $result->statustext);
}
+ or 00 prefixes).throttle middleware for API calls.Log::debug('Kavenegar response', ['data' => $result]);
$this->mock(Hosein\Kavenegar\Facades\Kavenegar::class)->shouldReceive('send')->once();
API Key Exposure
.env and validate it exists in AppServiceProvider:
if (!config('kavenegar.api_key')) {
throw new \RuntimeException('Kavenegar API key not configured.');
}
Phone Number Formatting
+ or 00). Strip prefixes:
$phone = Str::replace(['+', '00'], '', $user->phone);
Rate Limits
429 Too Many Requests:
try {
$result = Kavenegar::send(...);
} catch (\Kavenegar\Exceptions\ApiException $e) {
if ($e->getCode() === 429) {
sleep(5); // Retry after delay
retry();
}
}
Deprecated Methods
kavenegar/php library.No Queue Support
debug: true in config/kavenegar.php to log raw API responses.try-catch to catch ApiException for non-200 responses.config(['kavenegar.api_key' => env('KAVENEGAR_SANDBOX_KEY')]);
Custom Responses Extend the facade to add methods:
// app/Providers/AppServiceProvider.php
Kavenegar::macro('verify', function ($phone, $code) {
return $this->send($phone, "Code: {$code}");
});
Usage:
Kavenegar::verify('09123456789', '12345');
Event Dispatching Trigger events after sending:
// app/Providers/KavenegarServiceProvider.php
Kavenegar::extend(function ($api) {
$api->afterSend(function ($result) {
event(new SmsSent($result));
});
});
Override API Client
Replace the underlying KavenegarApi instance for custom logic:
$api = new \Kavenegar\KavenegarApi(config('kavenegar.api_key'));
$api->setCustomOption('timeout', 10); // Example
Kavenegar::setApi($api);
How can I help you explore Laravel packages today?