PHP code example of andrejro2 / yeastar-gsm-laravel

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

    

andrejro2 / yeastar-gsm-laravel example snippets


use AndrejRo2\LaravelYeastarSms\Facades\YeastarSms;
use AndrejRo2\LaravelYeastarSms\Exceptions\YeastarSmsException;

try {
    // Send SMS (connection configured via config/env)
    $success = YeastarSms::sendSms(
        port: 0,                        // GSM port (0-based)
        destination: '+1234567890',     // Phone number with country code
        message: 'Hello from Laravel!', // Message content
        id: 'SMS001'                   // Optional SMS ID for tracking
    );
    
    if ($success) {
        echo 'SMS sent successfully!';
    }
} catch (YeastarSmsException $e) {
    echo 'Error: ' . $e->getMessage();
}

use AndrejRo2\LaravelYeastarSms\Facades\YeastarSms;

// Configure connection manually
YeastarSms::setConnection('192.168.1.1:5038', 'admin', 'secret');

// Send SMS
$success = YeastarSms::sendSms(0, '+1234567890', 'Hello World!');

use AndrejRo2\LaravelYeastarSms\Contracts\YeastarSmsInterface;

class SmsController extends Controller
{
    public function sendSms(YeastarSmsInterface $yeastarSms)
    {
        try {
            $success = $yeastarSms->sendSms(0, '+1234567890', 'Hello via DI!');
            
            return response()->json(['success' => $success]);
        } catch (YeastarSmsException $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}

// Use a specific gateway configuration
$gatewayConfig = config('yeastar-sms.gateways.production');

YeastarSms::setConnection(
    $gatewayConfig['host'],
    $gatewayConfig['username'],
    $gatewayConfig['secret']
);

$success = YeastarSms::sendSms(0, '+1234567890', 'Production SMS');

$phones = ['+1234567890', '+0987654321', '+1122334455'];
$message = 'Bulk SMS message';

foreach ($phones as $index => $phone) {
    try {
        $smsId = 'BULK_' . time() . '_' . $index;
        $success = YeastarSms::sendSms(0, $phone, $message, $smsId);
        
        if ($success) {
            echo "SMS sent to {$phone}\n";
        }
        
        // Add delay to prevent overwhelming the gateway
        usleep(500000); // 0.5 second delay
        
    } catch (YeastarSmsException $e) {
        echo "Failed to send SMS to {$phone}: " . $e->getMessage() . "\n";
    }
}

try {
    YeastarSms::sendSms(0, '+1234567890', 'Test message');
} catch (YeastarSmsException $e) {
    // Handle specific error types
    $message = $e->getMessage();
    
    if (str_contains($message, 'connection')) {
        // Connection failed - check network/hostname
    } elseif (str_contains($message, 'authentication')) {
        // Authentication failed - check credentials
    } elseif (str_contains($message, 'SMS command failed')) {
        // SMS sending failed - check gateway/SIM status
    }
}

return [
    // Default gateway configuration
    'default' => [
        'host' => env('YEASTAR_HOST', '192.168.1.1:5038'),
        'username' => env('YEASTAR_USERNAME', 'admin'),
        'secret' => env('YEASTAR_SECRET', 'admin'),
    ],
    
    // Connection timeouts
    'timeouts' => [
        'connection' => env('YEASTAR_CONNECTION_TIMEOUT', 30),
        'read' => env('YEASTAR_READ_TIMEOUT', 5),
    ],
    
    // Logging configuration
    'logging' => [
        'enabled' => env('YEASTAR_LOGGING_ENABLED', true),
        'channel' => env('YEASTAR_LOG_CHANNEL', 'default'),
    ],
    
    // Multiple gateway configurations
    'gateways' => [
        'production' => [...],
        'staging' => [...],
        'development' => [...],
    ],
];
bash
php artisan vendor:publish --tag="yeastar-sms-config"
bash
# Basic usage
php artisan sms:send "+1234567890" "Hello from command line!"

# With options
php artisan sms:send "+1234567890" "Hello!" --port=0 --gateway=production --id=CMD001