PHP code example of fardadev / kavenegar-for-laravel
1. Go to this page and download the library: Download fardadev/kavenegar-for-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fardadev / kavenegar-for-laravel example snippets
return [
// Your Kavenegar API key (', ''),
// Default sender line number (optional)
'sender' => env('KAVENEGAR_SENDER', null),
// HTTP request timeout in seconds
'timeout' => env('KAVENEGAR_TIMEOUT', 30),
// Skip SMS sending in local/dev environments
'skip_in_development' => env('KAVENEGAR_SKIP_IN_DEV', true),
// Test phone numbers (SMS skipped in testing environment)
'test_phone_numbers' => [
'09112223344',
],
// Verification templates (must be created in Kavenegar panel)
'templates' => [
'login' => env('KAVENEGAR_TEMPLATE_LOGIN', 'login-verify'),
'email_password' => env('KAVENEGAR_TEMPLATE_EMAIL_PASS', 'email-pass'),
'two_factor' => env('KAVENEGAR_TEMPLATE_2FA', 'email-2fa'),
],
];
use FardaDev\Kavenegar\Facades\Kavenegar;
// Send to single recipient
$result = Kavenegar::send(
receptor: '09123456789',
message: 'Hello from Kavenegar!'
);
// Send to multiple recipients
$result = Kavenegar::send(
receptor: ['09123456789', '09987654321'],
message: 'Hello everyone!'
);
// Send with all options
$result = Kavenegar::send(
receptor: '09123456789',
message: 'Scheduled message',
sender: '10004346',
date: time() + 3600, // Send in 1 hour
type: 1, // Save to phone memory
localid: [123], // For duplicate prevention
hide: 1, // Hide receptor in logs
tag: 'marketing',
policy: 'custom-flow'
);
// Check result
foreach ($result as $response) {
echo "Message ID: {$response->messageid}\n";
echo "Status: {$response->statustext}\n";
echo "Cost: {$response->cost} Rials\n";
if ($response->isDelivered()) {
echo "Message delivered!\n";
} elseif ($response->isPending()) {
echo "Message is pending...\n";
}
}
use FardaDev\Kavenegar\Client\KavenegarClient;
class NotificationService
{
public function __construct(private KavenegarClient $kavenegar) {}
public function sendWelcomeSMS(string $phone): void
{
$this->kavenegar->send(
receptor: $phone,
message: 'Welcome to our service!'
);
}
}
// Get account info
$info = Kavenegar::info();
echo "Credit: {$info->remaincredit} Rials\n";
echo "Expiry: " . $info->getExpiryDate()->format('Y-m-d') . "\n";
if ($info->hasCredit()) {
echo "Account has credit\n";
}
if ($info->isExpired()) {
echo "Account is expired!\n";
}
// Get account configuration
$config = Kavenegar::config();
if ($config->hasApiLogsEnabled()) {
echo "API logs are enabled\n";
}
namespace App\Services;
use FardaDev\Kavenegar\Client\KavenegarClient;
use FardaDev\Kavenegar\Dto\MessageResponse;
class MyCustomSmsService
{
public function __construct(private readonly KavenegarClient $client) {}
public function sendOrderConfirmation(string $phone, string $orderNumber): MessageResponse
{
return $this->client->verifyLookup(
receptor: $phone,
template: 'order-confirmation',
token: $orderNumber
);
}
public function sendPasswordReset(string $phone, string $code, int $expiryMinutes): MessageResponse
{
return $this->client->verifyLookup(
receptor: $phone,
template: 'password-reset',
token: $code,
token2: (string) $expiryMinutes
);
}
// Add your own custom methods here
}
use App\Services\MyCustomSmsService;
class OrderController
{
public function __construct(private MyCustomSmsService $sms) {}
public function confirmOrder($orderId)
{
$order = Order::find($orderId);
$this->sms->sendOrderConfirmation($order->phone, $order->number);
}
}
// In config/kavenegar.php
'skip_in_development' => true,
// SMS will be skipped in local/dev environments
$helper = app(KavenegarHelper::class);
$result = $helper->sendLoginCode('09123456789', '123456');
// Returns true instead of sending actual SMS
// Check if would skip
if ($helper->shouldSkipInDevelopment('09123456789')) {
echo "SMS would be skipped in this environment";
}
$result = Kavenegar::send(
receptor: '09123456789',
message: 'Sensitive message',
hide: 1 // Receptor won't appear in sent message lists
);
// First send
$result = Kavenegar::send(
receptor: '09123456789',
message: 'Order #123 confirmed',
localid: [123]
);
// Duplicate attempt - won't send again
$result = Kavenegar::send(
receptor: '09123456789',
message: 'Order #123 confirmed',
localid: [123] // Same local ID
);
// Returns existing message details without resending