Laravel 框架的短信组件,用于轻松发送短信。

1.0.0 2019-12-21 17:40 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:46:09 UTC


README

此包为 Laravel 项目添加短信组件。它支持一些开箱即用的驱动程序,非常有用。

示例

SMS::to($user)
   ->content("Hi! Your order has been shipped!")
   ->send();

安装

您可以使用以下命令通过 composer 安装此包:

composer require "clzola/laravel-sms:^1.0.0"

包将自动注册。

您可以使用以下命令发布配置文件:

php artisan vendor:publish --provider="clzola\Components\Sms\SmsServiceProvider" --tag="config"

这是已发布配置文件的内容

return [

    /*
     * Specify which database driver you want to use.
     */
    'default' => env('SMS_DRIVER', 'null'),


    /*
     * Specify sender name.
     */
    'from' => env('SMS_FROM', 'Laravel'),


    /*
     * List of drivers and theirs configurations.
     */
    'drivers' => [

        /*
         * Driver for sending sms messages to running emulator.
         */
        'emulator' => [

            /*
             * Specify Android SDK path
             */
            'android_sdk_path' => env('SMS_ANDROID_SDK_PATH'),

        ]

    ]
    
];

使用方法

此包暴露了 SMS 门面。您指定消息的接收者和内容,然后调用 send()

SMS::to($user)->content($message)->send();

消息的接收者可以是有效的电话号码或任何实现 clzola\Components\Sms\Contracts\HasPhoneNumber 接口的实体。

示例

use clzola\Components\Sms\Contracts\HasPhoneNumber;

class Company extends Model implements HasPhoneNumber 
{
    // ...
    
    public function getPhoneNumber()
    {
        return $this->phone_number;
    }  
}

// ...

$company = Company::find(3);
SMS::to($company)->content($message)->send();

支持的驱动程序

空驱动程序

此驱动程序具有空的 send() 方法并丢弃所有消息。在测试和设置项目初期可能很有用。要使用此驱动程序,请将 sms.default 设置为 'null' 或在您的 .env 文件中将 SMS_DRIVER="null"

Android 模拟器驱动程序

此驱动程序可以向当前运行的模拟器发送短信消息。要使用此驱动程序,请将 sms.default 设置为 'emulator' 或在您的 .env 文件中将 SMS_DRIVER="emulator"。此外,不要忘记在您的 .env 文件中设置 Android SDK 路径 SMS_ANDROID_SDK_PATH="~/path/to/android/sdk"

Infobip 驱动程序

此驱动程序可以使用 Infobip 服务向物理设备发送实际短信消息。要使用此驱动程序,请将 sms.default 设置为 'infobip' 或在您的 .env 文件中将 SMS_DRIVER="infobip"。此外,在您的 config/services.php 中添加以下配置

"infobip" => [
    "api_key" => "YOUR_INFOBIP_API_KEY",
],

自定义驱动程序

目前此包支持一些驱动程序,但您可以注册自己的驱动程序。

首先确保您的驱动程序扩展了 clzola\Components\Sms\Drivers\Driver 类并实现了 send() 方法。

use clzola\Components\Sms\Drivers\Driver;

class CustomSmsDriver extends Driver 
{
    
    // ...
    
    public function send()
    {
        // Write code to send SMS message
    }
}

接下来,打开您的 AppServiceProvider 并在 boot() 方法中注册此驱动程序

class AppServiceProvider extends ServiceProvider
{
    // ...

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        app("sms")->registerDriver("custom", new CustomSmsDriver(...));
    }
}