webonaute/twilio-bundle

官方Twilio SDK v6的Symfony 5 / PHP 7包装器

安装次数: 3,039

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 45

类型:symfony-bundle

6.0.4 2021-03-30 18:15 UTC

This package is auto-updated.

Last update: 2024-09-13 00:02:03 UTC


README

#Symfony Twilio Bundle (for PHP SDK v5)

关于

快速简单地在基于Symfony的应用程序中使用Twilio SDK(版本6)。

支持PHP 7.4+和Symfony 5.x

有关如何使用Twilio Client的完整文档,请参阅由Twilio提供的官方SDK

感谢mblackford为这个包创建了支持SDK版本5的版本,感谢Fridolin Koch创建了支持SDK版本4的第一个版本。

安装

将以下内容添加到您的composer.json文件中

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

这将自动将twilio/sdk依赖项包含到您的项目中。

将包添加到app/AppKernel.php

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

配置

将以下内容添加到您的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: 

使用

提供的服务

在控制器中

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.");
    }
}

在控制台命令中

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.");
    }
}

版权 / 许可证

LICENSE