kamran377/yii2-sms-twilio
Composer 安装命令:
composer require kamran377/yii2-sms-twilio
包简介
A Twilio SMS plugin for Yii2 based off my base package, Yii2 SMS, to make sending SMS messages as easy as emails!
README 文档
README
A Twilio SMS plugin for Yii2 based off my base package, Yii2 SMS, to make sending SMS messages as easy as emails!
Installation
The preferred way to install this extension is through composer
Either run
composer require --prefer-dist kamran377/yii2-sms-twilio
or add
"kamran377/yii2-sms-twilio": "~1.0"
to the require section of your application's composer.json file.
Then add a new sms component to your main-local.php (advanced) or web.php (basic) like so:
'sms' => [
'class' => 'kamran377\sms\twilio\Sms',
// Advanced app use '@common/sms', basic use '@app/sms'
'viewPath' => '@common/sms', // Optional: defaults to '@app/sms'
// send all sms to a file by default. You have to set
// 'useFileTransport' to false and configure the messageConfig['from'],
// 'sid', and 'token' to send real messages
'useFileTransport' => true,
'messageConfig' => [
'from' => '+15552221234', // Your Twilio number (full or shortcode)
],
// Find your Account Sid and Auth Token at https://twilio.com/console
'sid' => 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'token' => 'your_auth_token',
// Tell Twilio where to POST information about your message.
// @see https://www.twilio.com/docs/sms/send-messages#monitor-the-status-of-your-message
//'statusCallback' => 'https://example.com/path/to/callback', // optional
],
Note: This package does not provide a route for the
statusCallback. You will need to create your own route to handle this. It isn't necessary to send SMS messages with Twilio, and is used for deeper tracking of each message's status.
Usage
You can send SMS messages two ways. One uses a view file, just like how the mailer does, by passing it in the compose() call. Only difference is, you don't specify html/text array keys. Just pass the string, since text messages don't use html.
With a view file
In your controller/model use it like so:
Yii::$app->sms->compose('test-message', ['name' => 'Wade'])
//->setFrom('12345') // if not set in config, or to override
->setTo('+15558881234')
->send();
You will need a view file located where your viewPath points to. By default, it is @app/sms. You can see in the configuration above that we overrode it to @common/sms. This is similar to the location Yii2 Advanced uses for the email views, the "common" directory.
View File: common/sms/test-message.php (advanced) or /sms/test-message.php (basic)
Hello <?= $name ?> This is a test!
Thanks!
Without a view file
When sending a large amount of text messages quickly, you may want to skip the view file for performance reasons, or you just may find it overkill for your usage.
$name = "Wade";
Yii::$app->sms->compose()
//->setFrom('12345') // optional if set in main config
->setTo('+15558881234')
->setMessage("Hey {$name} this is a test!")
->send();
setMessage() is a nicename function, or alias, for setTextBody(). Since SMS messages only deal with text and not html, I felt this was cleaner. However, setTextBody() will work just the same if your familiar with the way the mailer handles things. Either is fine.
Determining a successful send
A boolean is returned, so it is simple:
$result = Yii::$app->sms->compose()
->setTo('+15558881234')
->setMessage("Hey {$name} this is a test!")
->send();
if ( $result === true ) {
echo 'SMS was sent!';
} else {
'Error sending SMS!';
}
How do I send an image?
After your compose() call, use ->setMediaUrl($urlToImage)
Do I have to call setFrom()?
You only have to use setFrom() if you did not specify a from number in the configuration, within the messageConfig array.
If you want to override the from address in your main configuration, then you can call setFrom() specifically.
If you only send from 1 number, just configure it and don't call it directly so you don't have to change it in a bunch of places later.
Tip: If you have multuple numbers, use the Yii2 params array for an all-in-one easy spot to modify the numbers later.
Where can I find error logs?
When an Exception is hit during sending, they will be recorded under @runtime/sms and named according to the type of exception.
TODO:
- Update README
- Update documentation/commenting
- Create Twilio logger component
- Add support for more Twilio features (messagingServiceSid, channel address, etc)
Donate
Please consider donating if you find my code useful.
kamran377/yii2-sms-twilio 适用场景与选型建议
kamran377/yii2-sms-twilio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 121 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sms」 「twilio」 「yii2」 「text message」 「yii2 sms」 「yii2 twilio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kamran377/yii2-sms-twilio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kamran377/yii2-sms-twilio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kamran377/yii2-sms-twilio 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
SDK for payment gateway PlatbaMobilom.sk for PHP7.0
Componente para integração com serviços de mensageria com APIs externas como Twilio, SendGrid, AWS SES, etc.
Extensible library for building notifications and sending them via different delivery channels.
yii2-sms expand
统计信息
- 总下载量: 121
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-08