apilitylabs/keysms

v1.2.0 2024-09-17 10:31 UTC

This package is auto-updated.

Last update: 2024-09-17 10:33:01 UTC


README

此包实现了与KeySMS API交互的流畅接口。

安装

composer require apilitylabs/keysms

Laravel

在您的.env文件或环境中配置以下变量:

KEYSMS_USERNAME=<your username>
KEYSMS_API_KEY=<your api key>

如果您有一个验证过的发送者别名,并且想要将其全局配置为默认发送者

KEYSMS_DEFAULT_SENDER="Acme Inc"

使用方法

<?php

use KeySMS\SMS;

SMS::to('+4781549300')
    ->from('Acme Inc')
    ->message('Hello, World!');

当应用程序终止时,消息将自动调用send()方法,就像在Laravel中工作调度器的工作方式一样。您也可以显式调用此方法立即发送消息。

您可以传递多个接收者

SMS::to(['+4781549300', '+4799999999']);

我们建议您实现KeySMS\Contracts\PhoneNumber接口到您的用户模型中。然后,您可以直接将用户模型传递到该方法中

$user = Auth::user();

SMS::to($user)->message('Hello!');

通知

此包提供了一个Laravel通知通道,您可以使用。

在您的通知中启用通道,并实现toSMS($notifiable)方法

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

use KeySMS\SMS;

class HelloWorld extends Notification implements ShouldQueue
{
    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['keysms'];
    }

    public function toSMS($notifiable)
    {
        return 'Hello, World!';
    }
}

然后您可以将通知像平常一样发送出去

<?php

use App\Notifications\HelloWorld;

$user = Auth::user();

$user->notify(new HelloWorld);

PHP

此库可以在没有Laravel的情况下独立使用。

使用方法

<?php

use KeySMS\Facades\KeySMS;
use KeySMS\SMS;

// Called once to initialise the KeySMS client
KeySMS::init(<your username>, <your api key>);

SMS::to('+4781549300')
    ->message('Hello, World!')
    ->send();

版权所有 © ApilityLabs 2024