Service provider for Laravel for the Vonage PHP SDK
This is a Laravel Service Provider for integrating the Vonage PHP Client Library.
This Package is for use with Laravel versions 6.0 and upwards.
Using Composer, run the terminal command:
composer require vonage/laravel
By default, this package uses vonage/client, which includes a Guzzle adapter for
accessing the API. Some other libraries supply their own Guzzle adapter, leading
to composer not being able to resolve a list of dependencies. You may get an
error when adding
vonage/laravel to your application because of this.
The Vonage client allows you to override the HTTP adapter that is being used.
This takes a bit more configuration, but this package allows you to use
vonage/client-core to supply
your own HTTP adapter.
To do this:
composer require vonage/client-core to install the Core SDK with Composer.
Install your own httplug-compatible adapter. For example, to use Symfony's HTTP Client:
composer require symfony/http-client php-http/message-factory php-http/httplug nyholm/psr7
composer require vonage/laravel to install this package
In your .env file, add the following configuration:
You can now pull the Vonage\Client object from the Laravel Service Container, or use the Facade provided by this package.
You can use
artisan vendor:publish to copy the distribution configuration file to your app's
php artisan vendor:publish
config/vonage.php with your credentials. Alternatively, you can update your
with the following:
Optionally, you could also set an
private_key if required:
Private keys can either be a path to a file, like above, or the string of the key itself:
VONAGE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n[...]\n-----END PRIVATE KEY-----\n"
VONAGE_PRIVATE_KEY="-----BEGIN PRIVATE KEY----- [...] -----END PRIVATE KEY----- "
To use the Vonage Client Library you can use the Facade, or request the instance from the service container:
$text = new \Vonage\SMS\Message\SMS($toNumber, $fromNumber, 'Test SMS using Laravel'); Vonage::sms()->send($text);
$vonage = app('Vonage\Client'); $text = new \Vonage\SMS\Message\SMS($toNumber, $fromNumber, 'Test SMS using Laravel'); $vonage->sms()->send($text);
If you're using private key authentication, you can make a voice call:
$outboundCall = new \Vonage\Voice\OutboundCall( new \Vonage\Voice\Endpoint\Phone('14843331234'), new \Vonage\Voice\Endpoint\Phone('14843335555') ); $outboundCall ->setAnswerWebhook( new \Vonage\Voice\Webhook('https://example.com/answer') ) ->setEventWebhook( new \Vonage\Voice\Webhook('https://example.com/event') ) ; $response = Vonage::voice()->createOutboundCall($outboundCall);
For more information on using the Vonage client library, see the official client library repository.