composer require connect-corp/nexmo-client
.env:
NEXMO_API_KEY=your_api_key_here
NEXMO_API_SECRET=your_api_secret_here
config/app.php under providers:
ConnectCorp\NexmoClient\NexmoServiceProvider::class,
use ConnectCorp\NexmoClient\Facades\Nexmo;
$response = Nexmo::message()->send('1234567890', '15551232020', 'Hello from Laravel!');
Message Sending:
Nexmo::message()->send() for text messages.$response = Nexmo::message()
->to('15551232020')
->from('1234567890')
->text('Hello!')
->send();
Account Management:
Nexmo::account()->balance().Nexmo::number()->status('1234567890').Error Handling:
try-catch blocks to handle Nexmo\Exception:
try {
$response = Nexmo::message()->send(...);
} catch (Nexmo\Exception $e) {
Log::error('Nexmo error: ' . $e->getMessage());
return response()->json(['error' => 'Failed to send SMS'], 500);
}
Laravel Queues:
dispatch(new SendSmsJob($to, $from, $message));
SendSmsJob with Nexmo::message()->send().Logging:
Log::debug('Nexmo response:', ['response' => $response]);
Testing:
Nexmo::fake() in PHPUnit to mock responses:
public function test_sms_sending()
{
Nexmo::fake()->shouldReceive('send')->once()->andReturn(['status' => '0']);
$this->assertTrue(true); // Test logic here
}
Deprecated Methods:
invoke() instead of deprecated methods like send() where applicable.$nexmo->message->invoke($from, $to, 'text', $text) over $nexmo->message->send().Rate Limits:
use Symfony\Component\Process\Exception\TimeoutException;
try {
$response = $nexmo->message->invoke(...);
} catch (TimeoutException $e) {
sleep(2); // Exponential backoff logic here
retry();
}
Number Formatting:
+15551232020). Use a helper:
function formatPhoneNumber($number) {
return preg_replace('/[^0-9]/', '', $number);
}
Enable Debug Mode:
NEXMO_DEBUG=true in .env to log raw API responses.Common Errors:
apiKey and apiSecret.Custom Responses:
class CustomNexmoClient extends \Nexmo\Client {
public function verify($number, $code) {
return $this->request('POST', '/verify/json', [
'api_key' => $this->apiKey,
'api_secret' => $this->apiSecret,
'number' => $number,
'code' => $code,
]);
}
}
Middleware:
$nexmo->getClient()->getHandler()->push(new \GuzzleHttp\HandlerStack(), 'logging');
Configuration:
config/nexmo.php:
'timeout' => 30, // Default: 10 seconds
'base_uri' => env('NEXMO_BASE_URI', 'https://api.nexmo.com'),
How can I help you explore Laravel packages today?