Laravel Twillio API Integration
Begin by installing this package through Composer. In Laravel 4, run this command from the Terminal:
composer require aloha/twilio
In Laravel 5, use 2.0.0 pre-release version:
composer require 'aloha/twilio:2.0.0-RC2'
To wire this up in your Laravel project, wether it's built in Laravel 4 or 5, you need to add the service provider. Open app.php
, and add a new item to the providers array.
'Aloha\Twilio\Support\Laravel\ServiceProvider',
This will register two new artisan commands for you:
twilio:sms
twilio:call
Then, add a Facade for more convenient usage. In your app.php
config file add the following line to the aliases
array:
'Twilio' => 'Aloha\Twilio\Support\Laravel\Facade',
In Laravel 4 you need to publish the default config file to app/config/packages/aloha/twilio/config.php
with the artisan command config:publish aloha/twilio
.
In Laravel 5 you need to publish the default config file to config/twilio.php
with the artisan command vendor:publish
.
The facade now has the exact same methods as the Aloha\Twilio\TwilioInterface
.
One extra feature is that you can define which settings (and which sender phone number) to use:
Twilio::from('callcenter')->message($user->phone, $message);
Twilio::from('board_room')->message($boss->phone, 'Hi there boss!');
Define multiple entries in your twilio
config to make use of this feature.
Creating a Twilio object. This object implements the Aloha\Twilio\TwilioInterface
.
$twilio = new Aloha\Twilio\Twilio($accountId, $token, $fromNumber);
Sending a text message:
$twilio->message('+18085551212', 'Pink Elephants and Happy Rainbows');
Creating a call:
$twilio->call('+18085551212', 'http://foo.com/call.xml');
Generating a call and building the message in one go:
$twilio->call('+18085551212', function ($message) {
$message->say('Hello');
$message->play('https://api.twilio.com/cowbell.mp3', ['loop' => 5]);
});
Generating TwiML:
$twiml = $twilio->twiml(function($message) {
$message->say('Hello');
$message->play('https://api.twilio.com/cowbell.mp3', array('loop' => 5));
});
print $twiml;
laravel-twilio is open-sourced software licensed under the MIT license