webonaute/twilio-bundle

Symfony 5 / PHP 7 wrapper for the official Twilio SDK v6

Installs: 3 041

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 45

Type:symfony-bundle

6.0.4 2021-03-30 18:15 UTC

This package is auto-updated.

Last update: 2024-12-13 00:41:26 UTC


README

#Symfony Twilio Bundle (for PHP SDK v5)

About

A quick and easy way to use the Twilio SDK (version 6) in a Symfony based application.

Support for PHP 7.4+ and Symfony 5.x

For full documentation about how to use the Twilio Client, see the official SDK provided by Twilio.

Thank you for the awesome work of mblackford who created the version of this bundle, with support for version 5 of the SDK. 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.

Installation

Add this to your composer.json file:

"require": {
	"webonaute/twilio-bundle": "~6.0",
}

This will automatically include the twilio/sdk dependency into your project.

Add the bundle to app/AppKernel.php

$bundles = array(
	// ... other bundles
	new Webonaute\TwilioBundle\WebonauteTwilioBundle(),
);

Configuration

Add this to your config.yml:

blackford_twilio:
    # (Required) Username to authenticate with, typically your Account SID from www.twilio.com/user/account
    username: 'TODO'
    
    # (Required) Password to authenticate with, typically your Auth Token from www.twilio.com/user/account
    password: 'TODO'
    
    # (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: 

Usage

Provided services:

Inside a controller:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Twilio\Rest\Client;

class TestController extends Controller
{
    public function smsAction()
    {
        /** @var \Twilio\Rest\Client */
    	$twilio = $this->get('twilio.client');
        
        $date = date('r');
        
        $message = $twilio->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.");
    }
}

Inside a console command:

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Twilio\Rest\Client;

class TwilioTestCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('twilio:test:sms')
            ->setDescription('Test the Twilio integration by sending a text message.')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        //** @var \Twilio\Rest\Client */
        $twilio = $this->get('twilio.client');
         
         $date = date('r');
         
         $message = $twilio->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.");
    }
}

Copyright / License

See LICENSE