blackford / twilio-bundle
Symfony 5+ / PHP 8+ 的官方 Twilio SDK v6+ 的封装
v5.0.1
2016-11-27 23:54 UTC
Requires
- php: >=5.3.2
- symfony/framework-bundle: >=2.8
- twilio/sdk: ~5.0
Requires (Dev)
- phpunit/phpunit: ~4.1
- satooshi/php-coveralls: ~1.0
README
关于
快速简单地使用 Twilio SDK(版本 6)的 Symfony 基础应用方式。支持 PHP 8+,Symfony >= 5.4。有关如何使用 Twilio 客户端的完整文档,请参阅由 Twilio 提供的 官方 SDK。
此包是官方 SDK 的一个小型封装。它只添加基本的身份验证和配置,避免您手动配置 service.yaml。
如果您已经使用 symfony/notifier 组件,请查看 twilio-notifier。
感谢 Fridolin Koch 的杰出工作,他创建了此包的第一个版本,支持 SDK 的版本 4。
安装
composer req blackford/twilio-bundle
配置
在 .env.local
中添加这两个参数,并设置您的 Twilio 凭据。有关更多详细信息,请参阅 环境变量。
TWILIO_USER='!changeMe!' TWILIO_PASSWORD='!changeMe!'
添加一个新文件 config/packages/twilio.yml
并复制以下内容
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:
用法
提供的服务
在控制器内部
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."); } }
版权 / 许可证
请参阅 LICENSE