fresh / sinch-bundle
提供与Sinch.com短信API的集成。
0.4.0
2018-01-02 08:09 UTC
Requires
- php: ^7.1.3
- guzzlehttp/guzzle: ^6.3
- symfony/config: ^4.0
- symfony/event-dispatcher: ^4.0
- symfony/form: ^4.0
- symfony/framework-bundle: ^4.0
Requires (Dev)
README
📦 提供与 Sinch.com 短信API的集成。
目前处于开发过程中!任何时刻都可能发生变化!
要求
- PHP 7.1 及更高版本
- Symfony 4.0 及更高版本
安装
为Sinch创建应用程序
通过Composer添加依赖
composer req fresh/sinch-bundle='dev-master'
将密钥和密钥添加到parameters.yml
将以下行添加到您的parameters.yml.dist
文件中
parameters: sinch.key: EnterKeyForYourSinchApp sinch.secret: EnterSecretForYourSinchApp
在composer update过程中,您需要输入自己的Sinch应用程序的密钥和密钥,您可以在Sinch仪表板中找到。
更新config.yml
将以下行添加到config.yml
文件中
fresh_sinch: key: "%sinch.key%" secret: "%sinch.secret%"
使用方法
发送短信示例
use Fresh\SinchBundle\Service\Sinch; class Foo { public function bar(Sinch $sinch) { // Set the outbound number where you want to send the SMS $messageId = $sinch->sendSMS('+13155555552', 'Your message'); // If success then the ID of sent message is returned (it is an integer value) echo $messageId; } }
检查短信状态示例
use Fresh\SinchBundle\Service\Sinch; class Foo { public function bar(Sinch $sinch) { $messageId = 123456789; // The ID of Sinch message you get after successful SMS sending // Status is a string with one of these values: pending, successful, faulted, unknown $status = $sinch->getStatusOfSMS($messageId); // Other helper methods, return true of false $sinch->smsIsSentSuccessfully($messageId); $sinch->smsIsPending($messageId); $sinch->smsIsFaulted($messageId); $sinch->smsInUnknownStatus($messageId); } }
捕获和处理Sinch异常
use Fresh\SinchBundle\Exception\PaymentRequired\SinchPaymentRequiredException; use Fresh\SinchBundle\Service\Sinch; class Foo { public function bar(Sinch $sinch) { try { $messageId = $sinch->sendSMS($phoneNumber, 'Your message'); // Some logic related to SMS processing... } catch (SinchPaymentRequiredException $e) { $logger->error('SMS was not sent. Looks like your Sinch account run out of money.'); // Here you can, for example, send urgent emails to admin users // to notify that your Sinch account run out of money } } }
贡献
见CONTRIBUTING文件。