PHP code example of varaai / varasms

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

    

varaai / varasms example snippets


use VaraSMS\Laravel\Facades\VaraSMS;

// Send to a single recipient
$response = VaraSMS::sendSMS('255738234345', 'Hello World!');

// Send with custom sender ID and reference
$response = VaraSMS::sendSMS(
    '255738234345',
    'Hello World!',
    'MYSENDER',
    'ref123'
);

// Send to multiple recipients
$response = VaraSMS::sendSMS(
    [
        '255738234345',
        '255716718040'
    ],
    'Hello everyone!',
    'MYSENDER',
    'ref123'
);

// Single recipient response
[
    'success' => true,
    'message' => 'Message sent successfully',
    'data' => [
        'message_id' => '123456',
        'to' => '255738234345',
        'status' => 'SENT',
        'reference' => 'ref123'
    ]
]

// Multiple recipients response
[
    'success' => true,
    'message' => 'Messages sent successfully',
    'data' => [
        [
            'message_id' => '123456',
            'to' => '255738234345',
            'status' => 'SENT',
            'reference' => 'ref123'
        ],
        [
            'message_id' => '123457',
            'to' => '255716718040',
            'status' => 'SENT',
            'reference' => 'ref123'
        ]
    ]
]

use VaraSMS\Laravel\Facades\VaraSMS;

$messages = [
    [
        'to' => '255738234345',
        'message' => 'Hello User 1!',
        'reference' => 'ref1'
    ],
    [
        'to' => '255738234346',
        'message' => 'Hello User 2!',
        'reference' => 'ref2'
    ]
];

$response = VaraSMS::sendBulkSMS($messages);

use VaraSMS\Laravel\Facades\VaraSMS;

// Get all delivery reports
$reports = VaraSMS::getDeliveryReports();

// Get delivery report for a specific message ID
$report = VaraSMS::getDeliveryReport('28089492984101631440');

// Get delivery reports by date range (Deprecated)
$reports = VaraSMS::getDeliveryReportsByDateRange('2024-03-01', '2024-03-15');

use VaraSMS\Laravel\Facades\VaraSMS;

$balance = VaraSMS::getBalance();

use VaraSMS\Laravel\Facades\VaraSMS;

$response = VaraSMS::rechargeCustomer('[email protected]', 5000);

use VaraSMS\Laravel\Facades\VaraSMS;

$response = VaraSMS::deductCustomer('[email protected]', 2000);

use VaraSMS\Laravel\Facades\VaraSMS;

// Get all SMS logs
$logs = VaraSMS::getSMSLogs();

// Get SMS logs with filters
$logs = VaraSMS::getSMSLogs([
    'from' => 'NEXTSMS',          // Filter by sender ID
    'to' => '255716718040',       // Filter by recipient number
    'sentSince' => '2024-03-01',  // Filter messages sent from this date
    'sentUntil' => '2024-03-15',  // Filter messages sent until this date
    'limit' => 500,               // Limit number of records (max 500)
    'offset' => 20,               // Skip first 20 records
    'reference' => 'ref123'       // Filter by message reference
]);

[
    'success' => true,
    'logs' => [
        [
            'message_id' => '123456',
            'sender' => 'NEXTSMS',
            'recipient' => '255738234345',
            'message' => 'Hello World!',
            'status' => 'SENT',
            'timestamp' => '2024-03-24 10:30:00',
            'reference' => 'ref123'
        ],
        // ... more logs
    ],
    'pagination' => [
        'total' => 100,
        'offset' => 20,
        'limit' => 500
    ]
]

use VaraSMS\Laravel\Facades\VaraSMS;

$response = VaraSMS::registerSubCustomer([
    'first_name' => 'Api',
    'last_name' => 'Customer',
    'username' => 'apicust',
    'email' => '[email protected]',
    'phone_number' => '0738234339',  // Can be in format 0XXXXXXXXX or 255XXXXXXXXX
    'account_type' => 'Sub Customer (Reseller)', // or 'Sub Customer'
    'sms_price' => 20
]);

[
    'success' => true,
    'message' => 'Sub-customer registered successfully',
    'data' => [
        'id' => 123,
        'first_name' => 'Api',
        'last_name' => 'Customer',
        'username' => 'apicust',
        'email' => '[email protected]',
        'phone_number' => '255738234339',
        'account_type' => 'Sub Customer (Reseller)',
        'sms_price' => 20,
        'created_at' => '2024-03-24 10:30:00'
    ]
]

[
    'success' => true,
    'message' => 'Message sent successfully',
    'reference' => 'ref123'
]

[
    'success' => true,
    'reports' => [
        [
            'message_id' => '123456',
            'status' => 'DELIVERED',
            'recipient' => '255738234345',
            'timestamp' => '2024-03-24 10:30:00'
        ],
        // ... more reports
    ]
]

[
    'sms_balance' => 5000
]

[
    'success' => true,
    'status' => 200,
    'message' => 'Transaction completed successfully',
    'result' => [
        'Customer' => '[email protected]',
        'Sms transferred' => 5000,
        'Your sms balance' => 450000
    ]
]

try {
    $response = VaraSMS::sendSMS('255738234345', 'Hello World!');
} catch (\Exception $e) {
    // Handle the error
    Log::error('SMS sending failed: ' . $e->getMessage());
}

use VaraSMS\Laravel\Facades\VaraSMS;

class YourTest extends TestCase
{
    public function test_sends_sms()
    {
        VaraSMS::shouldReceive('sendSMS')
            ->once()
            ->with('255738234345', 'Test message')
            ->andReturn([
                'success' => true,
                'message' => 'Message sent successfully'
            ]);

        // Your test code here
    }
}

use VaraSMS\Laravel\Facades\VaraSMS;

// Schedule a one-time SMS
$response = VaraSMS::scheduleSMS(
    '255716718040',
    'Your message',
    '2024-03-25',  // Date in Y-m-d format
    '14:30',       // Time in 24-hour format
    'NEXTSMS'      // Optional sender ID
);

// Schedule a recurring SMS
$response = VaraSMS::scheduleSMS(
    '255716718040',
    'Your recurring message',
    '2024-03-25',  // Initial send date
    '14:30',       // Time to send
    'NEXTSMS',     // Optional sender ID
    [
        'repeat' => 'daily',           // hourly, daily, weekly, or monthly
        'start_date' => '2024-03-25',  // Optional start date for recurring
        'end_date' => '2024-04-25'     // Optional end date for recurring
    ]
);

[
    'success' => true,
    'message' => 'Message scheduled successfully',
    'data' => [
        'message_id' => '123456',
        'to' => '255716718040',
        'schedule_time' => '2024-03-25 14:30:00',
        'repeat' => 'daily',           // Only for recurring messages
        'start_date' => '2024-03-25',  // Only for recurring messages
        'end_date' => '2024-04-25'     // Only for recurring messages
    ]
]

use VaraSMS\Laravel\Facades\VaraSMS;

// Send different messages to different groups of recipients
$response = VaraSMS::sendMultipleMessages([
    [
        'to' => [
            '255716718040',
            '255758483019'
        ],
        'text' => 'First message to group 1',
        'from' => 'NEXTSMS'  // Optional sender ID
    ],
    [
        'to' => [
            '255758483019',
            '255655912841',
            '255716718040'
        ],
        'text' => 'Second message to group 2'
        // Using default sender ID from config
    ]
], 'batch123');  // Optional global reference


[
    'success' => true,
    'message' => 'Messages sent successfully',
    'data' => [
        [
            'message_id' => '123456',
            'to' => ['255716718040', '255758483019'],
            'status' => 'SENT',
            'text' => 'First message to group 1'
        ],
        [
            'message_id' => '123457',
            'to' => ['255758483019', '255655912841', '255716718040'],
            'status' => 'SENT',
            'text' => 'Second message to group 2'
        ]
    ],
    'reference' => 'batch123'
]
bash
php artisan vendor:publish --provider="VaraSMS\Laravel\VaraSMSServiceProvider"