cloudloyalty / smsaero-client
smsaero.ru API 客户端库
v1.6
2023-12-18 13:26 UTC
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^6.0 || ^7.0
- jms/serializer: ^1.4 || ^2.0 || ^3.0
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^8.5.23 || ^9.0
This package is auto-updated.
Last update: 2024-09-21 22:12:25 UTC
README
用于连接 SMS Aero 服务的库。
依赖关系
- PHP 7.2 及以上版本
- guzzlehttp
可以使用不使用 Guzzle 包的自定义客户端实现。
安装
$ php composer.phar require cloudloyalty/smsaero-client
示例
<?php use Feech\SmsAero\Auth\Auth; use Feech\SmsAero\Client\ClientGuzzle; use Feech\SmsAero\Exception\BaseSmsAeroException; use Feech\SmsAero\SmsAero; use Feech\SmsAero\Sms\Sms; $auth = new Auth('email', 'pass'); $client = new ClientGuzzle($auth); $smsAero = new SmsAero($client); $sms1 = new Sms('79591234567', 'Тестовое сообщение', Sms::CHANNEL_TYPE_INTERNATIONAL); $sms2 = new Sms(['79591234567', '79599876543'], 'Тестовое сообщение', Sms::CHANNEL_TYPE_DIGITAL); try { $smsAero->testSend($sms1); // тестовое сообщение $smsAero->send($sms1); // отправка сообщения $response = $smsAero->bulkSend($sms1); // массовая отправка сообщений $responseArray = json_decode($response, true); // ответ в виде ассоциативного массива } catch (BaseSmsAeroException $e) { $e->getMessage(); }
配置 Guzzle 适配器
use Feech\SmsAero\Auth\Auth; use Feech\SmsAero\Client\ClientGuzzle; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // configure required handler, middleware: $handlerStack = HandlerStack::create(); $guzzleClient = new Client([ 'connect_timeout' => 5, 'timeout' => 5, 'handler' => $handlerStack, ]); $auth = new Auth('email', 'pass'); $client = new ClientGuzzle($auth, $guzzleClient);
在 DTO 中反序列化响应
通过 DTO 使用 jms/serializer 和独立的 SmsAeroClient 类来处理服务响应。
use Feech\SmsAero\Auth\Auth; use Feech\SmsAero\Client\ClientGuzzle; use Feech\SmsAero\Sms\Sms; use Feech\SmsAero\SmsAeroClient; use JMS\Serializer\SerializerBuilder; $auth = new Auth('email', 'pass'); $client = new ClientGuzzle($auth); $serializer = SerializerBuilder::create()->build(); $smsAero = new SmsAeroClient($client, $serializer); $sms = Sms::toSingleNumber('79591234567', 'Тестовое сообщение', Sms::CHANNEL_TYPE_INTERNATIONAL); $result = $smsAero->send($sms); var_dump($result->data->status); var_dump($result->data->cost);