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
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'],
]);