tenko / link-sms

链团第三方短信SDK

v0.1.5 2023-06-09 06:37 UTC

This package is auto-updated.

Last update: 2024-09-09 09:15:48 UTC


README

LinkSMS是链团短信服务的一个第三方SDK。

中文文档

快速开始

  • 首先:创建一个配置对象
class Config implements \LinkSms\Library\ConfigInterface {
    public function getBaseUrl(): string
    {
        return 'https://sdk3.028lk.com:9988';
    }

    public function getCorpId(): string
    {
        return '[CORP_ID]';
    }

    public function getPassword(): string
    {
        return '[PASSWORD]';
    }

    public function getHeaders(): array
    {
        return [];
    }

    public function getGuzzleConfig(): array
    {
        return [];
    }
}

$config = new Config();

或者您可以使用这样的匿名类

$config = new class implements \LinkSms\Library\ConfigInterface {
    public function getBaseUrl(): string
    {
        return 'https://sdk3.028lk.com:9988';
    }

    public function getCorpId(): string
    {
        return '[CORP_ID]';
    }

    public function getPassword(): string
    {
        return '[PASSWORD]';
    }

    public function getHeaders(): array
    {
        return [];
    }

    public function getGuzzleConfig(): array
    {
        return [];
    }
};
  • 如果您需要记录事件,也提供了日志记录接口。以下是一个使用示例
$log = new class implements \LinkSms\Library\LogInterface {
    public function info(string $message, array $data = [])
    {
        // TODO write info
    }

    public function warning(string $message, array $data = [])
    {
        // TODO write warning
    }

    public function error(string $message, array $data = [])
    {
        // TODO write error
    }
};
  • 然后您就可以开始使用了。
$sms = new \LinkSms\SmsService($config[, $log]); // It is not necessary to pass a LogInterface

用法

  • 发送消息
$sms->sendMessage(
    ['13800008888'],
    'content 【Sign】',
    '', // Cell, must be a numeric string,
    new DateTimeImmutable('2024-01-01 08:00') // It is not necessary unless you need to schedule the sending of text messages.
)
  • 获取余额
$remain = $sms->getRemain();
  • 获取消息
/** @var array<\LinkSms\Library\Message> $messages */
$messages = $sms->fetchMessage();

foreach ($messages as $message) {
    $mobile = $message->getMobile();
    
    $content = $message->getContent();
    
    /** @var DateTimeImmutable $sendTime */
    $sendTime = $message->getSendTime();
    $sendTimestamp = $sendTime->getTimestamp();
    
    $cell = $message->getCell();
}