humans/semaphore-sms

一个集成了Laravel的Semaphore SMS客户端。

5.0.1 2023-11-23 19:22 UTC

This package is auto-updated.

Last update: 2024-09-23 21:29:07 UTC


README

Latest Stable Version License

这是一个Semaphore SMS服务提供商的PHP客户端,具有开箱即用的Laravel集成。

安装

通过composer安装

composer require humans/semaphore-sms

要开始使用库,我们必须提供Semaphore API密钥。

发送消息

$client = new Client(

  '<SEMAPHORE API KEY>',

  // If the sender is left blank, SEMAPHORE will be the default sender name.
  '<SENDER>',

);

$client->message()->send('0917xxxxxxx', '<Your message here>');

Laravel集成

如果您正在使用Laravel,我们为您设置了一些便利性。首先,您可以在.env文件中设置API密钥和发送者名称。

SEMAPHORE_API_KEY=xxxxxxxx
SEMAPHORE_SENDER_NAME=xxxxxxxx

如果您已禁用包发现功能,请确保在app.php中注册Humans\Semaphore\Laravel\ServiceProvider

发送消息

您可以使用由该包提供的Semaphore外观。

use Humans\Semaphore\Laravel\Facades\Semaphore;

Semaphore::message()->send(
    '0917xxxxxxx',
    '<Your message here>'
);

使用通知

如果您想使用Laravel的通知功能,此包提供了SemaphoreChannelSemaphoreMessage类。

配置可通知对象

在您的可通知类中,添加一个routeNotificationForSempahore方法并使用包含手机号的数据库列。

class User {
    use Notifiable;

    // ...

    public function routeNotificationForSemaphore()
    {
        return $this->mobile_number;
    }
}

使用通知

use Humans\Semaphore\Laravel\Contracts\UsesSemaphore;

class Welcome extends Notification implements UsesSemaphore
{
    public function via($notifiable): array
    {
        return [SemaphoreChannel::class];
    }

    public function toSemaphore($notifiable): SemaphoreMessage
    {
        return (new SemaphoreMessage)
            ->message('<Your message here>');
    }
}

User::first()->notify(new Welcome);

使用即时可通知对象

在需要发送消息但不需要模型的情况下,此包也支持Laravel的即时通知。

use Humans\Semaphore\Laravel\SemaphoreChannel;use Illuminate\Support\Facades\Notification;

Notification::route(SemaphoreChannel::class, '0917xxxxxxx')->notify(new Welcome);