leandro980 / skebby-bundle
通过Skebby服务发送短信的套餐
Requires
- php: >=7
- giggsey/libphonenumber-for-php: ^8.10
- guzzlehttp/guzzle: ^7.0
- instasent/sms-counter-php: ^0.5.2
- karriere/json-decoder: ^4.0
- symfony/framework-bundle: 5.4.*|^6.0
README
这是一个非官方的Symfony5套餐,用于Skebby短信服务提供商。
安装
建议的安装方法是使用 composer
$ composer require leandro980/skebby-bundle
配置
在 config/bundles.php
中启用套餐
return [ // ... // ... Szopen\SkebbyBundle\SkebbyBundle::class => ['all' => true], ];
在你的 config/packages/skebby_bundle.yaml
skebby: # Add your Skebby account credentials username: 'yourskebbyusername' password: 'yourskebbypassword' # Skebby provides two kinds of authentication: # - Getting a Session Id that expires in 5 minutes if no request is sent # - Getting a Token always valid # Allowed values are: # - token (default) # - session # # auth_type: 'token' # You can choose which kind of sms send between: # - "GP" for Classic+ (limited to 1530 chars, delivery warranty, delivery report) # - "TI" for Classic (limited to 1530 chars, delivery warranty) # - "SI" for Basic (limited to 160 chars, no delivery warranty) # # message_type: 'TI' # You can also add a default sender alias used to send SMS. # This overrides the default alias set in Skebby account but must be one of the alias # already registered. # If the message type allows a custom TPOA and the field is left empty, the user’s preferred TPOA # is used. Must be empty if the message type does not allow a custom TPOA. # # default_sender_alias: ""
简单Symfony使用
在你的控制器中可以访问 SkebbyManager
服务。
检查账户状态
use Szopen\SkebbyBundle\Model\Manager\SkebbyManager; // .. /** * @Route("/skebby/status", name="skebby.status") */ public function statusAction(SkebbyManager $skebby) { $s = $skebby->getStatus(); return $this->render('skebby/index.html.twig', [ 'status' => $s, ]); }
SkebbyManager::getStatus
返回一个 Szopen\SkebbyBundle\Model\Response\Status
发送短信
use Szopen\SkebbyBundle\Model\Manager\SkebbyManager; use Szopen\SkebbyBundle\Model\Data\Recipient; // .. /** * @Route("/skebby/send", name="skebby.sendsms") */ public function sendSmsAction(SkebbyManager $skebby) { $sms = $skebby->createDefaultSms('Hello, this is a test message', [new Recipient('3331234567'), new Recipient('3207654321')]); $response = $skebby->sendSms($sms); return $this->render('skebby/index.html.twig', [ 'response' => $response, ]); }
SkebbyManager::sendSms
返回一个 Szopen\SkebbyBundle\Model\Response\SmsResponse
文档
Skebby套餐使用一系列客户端类来执行特定的API调用。 SkebbyManager
是一个封装所有客户端方法的封装服务。请参考官方Skebby开发者文档以获取更多信息。
认证器
您可以通过 AuthenticatorFactory
类选择要使用的认证类型。
use Szopen\SkebbyBundle\Model\Auth\AuthenticatorFactory; //.. // Token authentication $auth = AuthenticatorFactory::create('token') // Session authentication // $auth = AuthenticatorFactory::create('session') // Used to authenticate in later API calls $arrayAccess = $auth->login('username', 'password');
如果没有执行API调用,会话认证持续5分钟。
UserClient
用于用户/账户API调用的专用类。
use Szopen\SkebbyBundle\Model\Auth\AuthenticatorFactory; use Szopen\SkebbyBundle\Model\Client\UserClient; //.. // Token authentication $auth = AuthenticatorFactory::create('token') $userClient = new UserClient('username', 'password', $auth);
您可以在 Szopen\SkebbyBundle\Model\Client\UserClient
的方法注释中了解更多信息
SmsClient
用于Sms API调用的专用类。
发送简单短信。
use Szopen\SkebbyBundle\Model\Auth\AuthenticatorFactory; use Szopen\SkebbyBundle\Model\Client\SmsClient; use Szopen\SkebbyBundle\Model\Data\Recipient; //.. // Token authentication $auth = AuthenticatorFactory::create('token') $smsClient = new SmsClient('username', 'password', $auth); // Creating an sms // You must choose which kind of SMS type to send $sms = new Sms(Sms::SMS_CLASSIC_KEY); // Add a message // SmsClient choose wich kind of encoding to use, between UCS2 and GSM, // authomatically parsing the message. // It also counts chars available based on encoding, if the ength of the message exceeds the limit // it raises a MessageLengthException $sms->setMessage("Hello, this is a GSM encoded message"); // Substitutes message $sms->setMessage("Hello, this is a UCS2 encoded message because of ç char"); // Set sender // You can add sender only if your not using BASIC SMS and the aliasis registered to your account $sms->setSender('YourAlias'); // Creating and adding a Recipient // When you create a "Recipient" it parses the phone number, if it's not valid it raises // a \libphonenumber\NumberParseException $recipient = new Recipient("+393211234567"); $sms->addRecipient($recipient); // Sending SMS $smsResponse = $smsClient->sendSms($sms); // You can choose to allow invalid recipients (that means that an invalid recipient // won't block the entire operation) and if you want have the remaining sms and credit $smsResponse = $smsClient->sendSms($sms, true, // Allow invalid recipients true, // Return remaining true, // Return credit );
您可以将短信发送到组。在这种情况下,收件人必须是Group类型
use Szopen\SkebbyBundle\Model\Auth\AuthenticatorFactory; use Szopen\SkebbyBundle\Model\Client\SmsClient; use Szopen\SkebbyBundle\Model\Data\Group; //.. // Creating and adding a recipient $recipient = new Group("groupname"); $sms->addRecipient($recipient); // Sending SMS $smsResponse = $smsClient->sendGroupSms($sms); // You can choose to allow invalid recipients (that means that an invalid recipient // won't block the entire operation) and if you want have the remaining sms and credit $smsResponse = $smsClient->sendGroupSms($sms, true, // Allow invalid recipients true, // Return remaining true, // Return credit );
还可以发送带参数的短信。您不能向组发送参数短信
// Adds a message with parameters // The system recognizes the parameters in the text $sms->setMessage('Hello ${name}, i know your surname is ${surname}'); // Creating and adding a Recipient with parameters $recipient = new Recipient("+393211234567"); $recipient->addVariable('name', 'John'); $recipient->addVariable('surname', 'Dorian'); $sms->addRecipient($recipient); // Sending SMS // If just a Recipient does'nt contains all the parameters defined in message // it raises a MissingParameterException $smsResponse = $smsClient->sendSms($sms);
您可以在 Szopen\SkebbyBundle\Model\Client\SmsClient
的方法注释中了解更多信息
SkebbyManager
该类配置为Symfony5服务,并封装所有客户端方法。它添加了 SkebbyManager::createDefaultSms
,该函数返回一个使用yaml文件中配置的所有默认参数的短信。
$sms = $skebby->createDefaultSms('Hello, this is a test message'); $sms->addRecipient(new Recipient("+393211234567")); // You can also add recipients in constructor $sms = $skebby->createDefaultSms('Hello, this is a test message', [new Recipient('3331234567'), new Recipient('3207654321')]);
您可以在 Szopen\SkebbyBundle\Model\Manager\SkebbyManager
的方法注释中了解更多信息
许可
MIT许可,有关更多信息,请参阅LICENSE