PHP code example of abather / sms4jawaly

1. Go to this page and download the library: Download abather/sms4jawaly 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/ */

    

abather / sms4jawaly example snippets


return [
    'api_key'            => env('SMS4JAWALY_API_KEY'),
    'api_secret'         => env('SMS4JAWALY_API_SECRET'),
    'default_sender'     => env('SMS4JAWALY_DEFAULT_SENDER', '4jawaly'),
    'receiver_attribute' => env('SMS4JAWALY_RECEIVER_ATTRIBUTE', 'phone'),
];

use Sms4jawaly\Laravel\Gateway;

$gateway = app(Gateway::class);

$response = $gateway->sendSms(
    'مرحباً بك في خدمتنا!',  // Message text | نص الرسالة
    ['966500000000'],          // Recipient numbers | أرقام المستلمين
    '4jawaly'                  // Sender name | اسم المرسل
);

// Response structure:
// [
//     'success'       => true,
//     'total_success' => 1,
//     'total_failed'  => 0,
//     'job_ids'       => ['job_id_here'],
//     'errors'        => []
// ]

use Sms4jawaly\Laravel\Facades\Sms4jawaly;

$response = Sms4jawaly::sendSms(
    'Your verification code is: 123456',
    ['966500000000', '966511111111'],  // Send to multiple numbers
    '4jawaly'
);

use Sms4jawaly\Laravel\Facades\Sms4jawaly;

$balance = Sms4jawaly::getBalance();

// Response structure:
// [
//     'success' => true,
//     'data'    => [...] // API response data
// ]

if ($balance['success']) {
    // Process balance data
    $packages = $balance['data'];
}

use Sms4jawaly\Laravel\Facades\Sms4jawaly;

$senders = Sms4jawaly::getSenders();

// Response structure:
// [
//     'success'         => true,
//     'all_senders'     => ['4jawaly', 'MySender'],
//     'default_senders' => ['4jawaly'],
//     'message'         => 'تم'
// ]

if ($senders['success']) {
    $allSenders = $senders['all_senders'];
    $defaultSenders = $senders['default_senders'];
}



namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Sms4jawaly\Laravel\Message;
use Sms4jawaly\Laravel\Sms4JawalyChannel;

class WelcomeNotification extends Notification
{
    public function via($notifiable)
    {
        return [Sms4JawalyChannel::class];
    }

    public function to4Jawaly($notifiable): Message
    {
        return Message::make("Welcome {$notifiable->name}!")
            ->phone($notifiable->phone)    // Optional: override phone number
            ->sender('4jawaly');           // Optional: override sender name
    }
}

Message::make('Your message text')
    ->phone('966500000000')  // Set recipient phone number
    ->sender('4jawaly')      // Set sender name
    ->message('New text');   // Update message text

public function to4Jawaly($notifiable): string
{
    return "Your verification code is: {$this->code}";
}

use App\Notifications\WelcomeNotification;

$user = User::find(1);
$user->notify(new WelcomeNotification());

public function routeNotificationFor($channel)
{
    if ($channel === 'sms-4-jawaly') {
        return $this->mobile_number; // Use a different attribute
    }

    return $this->phone;
}

use App\Notifications\WelcomeNotification;
use Illuminate\Support\Facades\Notification;

Notification::route('sms-4-jawaly', '966500000000')
    ->notify(new WelcomeNotification());

Notification::route('sms-4-jawaly', ['966500000000', '966511111111'])
    ->notify(new WelcomeNotification());

[
    'success'       => bool,    // Overall success status
    'total_success' => int,     // Number of successful sends
    'total_failed'  => int,     // Number of failed sends
    'job_ids'       => array,   // Array of job IDs from the API
    'errors'        => array    // Errors by error message => numbers
]

[
    'success' => bool,   // Success status
    'data'    => mixed,  // API response data
    'error'   => string  // Error message (if failed)
]

[
    'success'         => bool,     // Success status
    'all_senders'     => array,    // All available sender names
    'default_senders' => array,    // Default sender names
    'message'         => string,   // Success message
    'error'           => string    // Error message (if failed)
]

$response = Sms4jawaly::sendSms('Hello', ['966500000000'], '4jawaly');

if ($response['success']) {
    // Message sent successfully
    $jobIds = $response['job_ids'];
} else {
    // Handle errors
    foreach ($response['errors'] as $error => $numbers) {
        Log::error("Failed to send to " . implode(',', $numbers) . ": $error");
    }
}

$balance = Sms4jawaly::getBalance();

if (!$balance['success']) {
    Log::error('Failed to get balance: ' . $balance['error']);
}
bash
php artisan vendor:publish --provider="Sms4jawaly\Laravel\Sms4jawalyServiceProvider"
bash
php artisan make:notification WelcomeNotification