A quick and easy way to use the Twilio SDK (version 6) in a Symfony based application. Support for PHP 8+, Symfony >= 5.4. For full documentation about how to use the Twilio Client, see the official SDK provided by Twilio.
This bundle is a small wrapper around the official sdk. It just adds the basic auth and configs and avoid that you have to configure your service.yaml manual.
If you are already using the symfony/notifier component, habe a look at twilio-notifier.
Thank you for the awesome work of Fridolin Koch who created the first version of this bundle, with support for version 4 of the SDK.
composer req blackford/twilio-bundle
Add these 2 parameters in .env.local
and set it to your twilio credentials.
Please see Env-Variables for more details.
TWILIO_USER='!changeMe!'
TWILIO_PASSWORD='!changeMe!'
Add a new file config/packages/twilio.yml
and copy and adjust the following content:
blackford_twilio:
# (Required) Username to authenticate with, typically your Account SID from www.twilio.com/user/account
username: '%env(TWILIO_USER)%'
# (Required) Password to authenticate with, typically your Auth Token from www.twilio.com/user/account
password: '%env(TWILIO_PASSWORD)%'
# (Optional) Account Sid to authenticate with, defaults to <username> (typically not required)
# accountSid:
# (Optional) Region to send requests to, defaults to no region selection (typically not required)
# region:
Provided services:
Service | Class |
---|---|
twilio.client |
\Twilio\Rest\Client |
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Twilio\Rest\Client as TwilioClient;
class TestController extends Controller
{
public function __construct(private TwilioClient $twilioClient)
{}
public function smsAction()
{
$date = date('r');
$message = $this->twilioClient->messages->create(
'+12125551234', // Text any number
array(
'from' => '+14085551234', // From a Twilio number in your account
'body' => "Hi there, it's $date and Twilio is working properly."
)
);
return new Response("Sent message [$message->sid] via Twilio.");
}
}
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Twilio\Rest\Client as TwilioClient;
#[AsCommand(name: 'twilio:test:sms', description: 'Test the Twilio integration by sending a text message.')]
class TwilioTestCommand extends ContainerAwareCommand
{
public function __construct(private TwilioClient $twilioClient)
{}
protected function execute(InputInterface $input, OutputInterface $output)
{
$date = date('r');
$message = $this->twilioClient->messages->create(
'+12125551234', // Text any number
array(
'from' => '+14085551234', // From a Twilio number in your account
'body' => "Hi there, it's $date and Twilio is working properly."
)
);
$output->writeln("Sent message [$message->sid] via Twilio.");
}
}
See LICENSE