PHP code example of palpalani / laravel-bandwidth-api

1. Go to this page and download the library: Download palpalani/laravel-bandwidth-api 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/ */

    

palpalani / laravel-bandwidth-api example snippets


return [
    'messaging' => [
        'username' => env('BANDWIDTH_MESSAGING_USERNAME'),
        'password' => env('BANDWIDTH_MESSAGING_PASSWORD'),
        'account_id' => env('BANDWIDTH_MESSAGING_ACCOUNT_ID'),
        'application_id' => env('BANDWIDTH_MESSAGING_APPLICATION_ID'),
    ],

    'voice' => [
        'username' => env('BANDWIDTH_VOICE_USERNAME'),
        'password' => env('BANDWIDTH_VOICE_PASSWORD'),
        'account_id' => env('BANDWIDTH_VOICE_ACCOUNT_ID'),
    ],

    'twoFactor' => [
        'username' => env('BANDWIDTH_TWO_FACTOR_USERNAME'),
        'password' => env('BANDWIDTH_TWO_FACTOR_PASSWORD'),
        'account_id' => env('BANDWIDTH_TWO_FACTOR_ACCOUNT_ID'),
    ],

    'webRtc' => [
        'username' => env('BANDWIDTH_WEBRTC_USERNAME'),
        'password' => env('BANDWIDTH_WEBRTC_PASSWORD'),
        'account_id' => env('BANDWIDTH_WEBRTC_ACCOUNT_ID'),
    ],

    'dashboard' => [
        'username' => env('BANDWIDTH_DASHBOARD_USERNAME'),
        'password' => env('BANDWIDTH_DASHBOARD_PASSWORD'),
        'url' => env('BANDWIDTH_DASHBOARD_API_URL', 'https://dashboard.bandwidth.com/api/'),
    ],
];

use palPalani\Bandwidth\Facades\Bandwidth;

$result = Bandwidth::sendMessage(
    from: '+1234567890',
    to: ['+0987654321'],
    text: 'Hello from Laravel!'
);

if ($result['success']) {
    echo "Message sent! ID: {$result['id']}";
} else {
    echo "Error: {$result['error']}";
}

use palPalani\Bandwidth\Bandwidth;

class NotificationService
{
    public function __construct(
        private Bandwidth $bandwidth
    ) {}

    public function sendAlert(string $phone, string $message): array
    {
        return $this->bandwidth->sendMessage(
            from: config('app.sms_from_number'),
            to: [$phone],
            text: $message
        );
    }
}

$result = Bandwidth::sendMessage(
    from: '+1234567890',
    to: ['+0987654321'],
    text: 'Your verification code is: 123456',
    tag: 'verification-code'
);

[
    'success' => true,
    'message' => 'Message sent successfully.',
    'id' => 'message-id-from-bandwidth',
    'data' => [...] // Full API response object
]

[
    'success' => false,
    'message' => 'Failed to send message.',
    'error' => 'Detailed error message from API'
]

$result = Bandwidth::sendMessage(
    from: '+1234567890',
    to: ['+0987654321'],
    text: 'Test message'
);

if ($result['success']) {
    // Success - message sent
    $messageId = $result['id'];
    $fullResponse = $result['data'];

    // Store message ID for tracking
    \Log::info("SMS sent successfully", ['id' => $messageId]);
} else {
    // Error occurred
    $errorMessage = $result['error'];

    // Log error for debugging
    \Log::error("SMS failed", ['error' => $errorMessage]);

    // Notify user or retry
    throw new \Exception("Could not send SMS: {$errorMessage}");
}

$account = Bandwidth::getAccount();

// Now you can use the Iris\Account object to manage your account
// See Bandwidth IRIS API documentation for available methods

// Get the main Bandwidth client
$client = app('bandwidth');

// Access specific API clients
$messagingClient = $client->getMessaging();
$voiceClient = $client->getVoice();
$webRtcClient = $client->getWebRtc();
$mfaClient = $client->getMultiFactorAuth();
$lookupClient = $client->getPhoneNumberLookup();

// Access Dashboard/IRIS client
$irisClient = app('phone');

public function sendMessage(
    string $from,
    array $to,
    string $text,
    string|null $tag = null
): array

public function getAccount(): Iris\Account

use palPalani\Bandwidth\Facades\Bandwidth;

// In your tests
Bandwidth::shouldReceive('sendMessage')
    ->once()
    ->with('+1234567890', ['+0987654321'], 'Test message', null)
    ->andReturn([
        'success' => true,
        'message' => 'Message sent successfully.',
        'id' => 'test-message-id',
        'data' => (object) ['id' => 'test-message-id'],
    ]);

\Log::debug('Sending Bandwidth message', [
    'from' => $from,
    'to' => $to,
    'text' => $text,
]);

$result = Bandwidth::sendMessage($from, $to, $text);

\Log::debug('Bandwidth response', $result);

// Before (still works)
Bandwidth::sendMessage($from, $to, $text);

// After (optional tag parameter)
Bandwidth::sendMessage($from, $to, $text, 'my-tag');

// Returned mixed response or threw exceptions
$response = Bandwidth::sendMessage($from, $to, $text);

// Returns structured array
$result = Bandwidth::sendMessage($from, $to, $text);

if ($result['success']) {
    $messageId = $result['id'];
} else {
    $error = $result['error'];
}

Bandwidth::sendMessage($from, $to, $text, $applicationId);

// applicationId now comes from config
Bandwidth::sendMessage($from, $to, $text);
bash
php artisan vendor:publish --provider="palPalani\Bandwidth\BandwidthServiceProvider" --tag="laravel-bandwidth-api-config"
bash
composer analyse