fresh/sinch-bundle

提供与Sinch.com短信API的集成。

资助包维护!
fre5h

安装: 789

依赖项: 0

建议者: 0

安全: 0

星标: 16

关注者: 2

分支: 1

开放问题: 2

类型:symfony-bundle

0.4.0 2018-01-02 08:09 UTC

This package is auto-updated.

Last update: 2024-08-30 14:57:57 UTC


README

📦 提供与 Sinch.com 短信API的集成。

目前处于开发过程中!任何时刻都可能发生变化!

Sinch Logo

Scrutinizer Quality Score Build Status CodeCov License Latest Stable Version Total Downloads StyleCI Gitter

SensioLabsInsight knpbundles.com

要求

  • PHP 7.1 及更高版本
  • Symfony 4.0 及更高版本

安装

为Sinch创建应用程序

Sinch.com注册并创建一个新应用

通过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文件。