主要短信网关的短信封装器

v2.1.7 2019-08-14 06:21 UTC

README

Latest Stable Version Latest Unstable Version License Total Downloads

简介

Elimuswift SMS 是一个用于通过各种 SMS 提供商发送 SMS 的包。这个用于 Laravel 的包增加了从您的 Web 应用程序发送和接收 SMS/MMS 消息到手机的功能。该包支持 Africas Talking

要求

Laravel 5

  • PHP: >= 5.5
  • Guzzle >= 6.0

配置

Composer

首先,将 Simple SMS 包添加到您的 composer/json 文件中的 require

"require": {
    "elimuswift/sms": "~2.0"
}

接下来,运行 composer update 命令。这将安装该包到您的 Laravel 应用程序中。

服务提供者

一旦您将包添加到 composer 文件中,您需要将服务提供者注册到 Laravel。

	Elimuswift\SMS\Providers\SmsServiceProvider::class,

别名

最后,注册 Facade。

'SMS' => Elimuswift\SMS\Facades\SMS::class,

API 设置

您必须运行以下命令来保存您的配置文件到本地应用

 php artisan vendor:publish --provider="Elimuswift\SMS\Providers\SmsServiceProvider"

这将复制配置文件到您的 config 文件夹。

入门

Africas Talkig

要启用 AfricasTalking 驱动程序,只需将配置文件更改为

'driver' => env('SMS_DRIVER', 'africastalking'),
'africastalking' => [
        'api_key' => env('AT_API_KEY', 'africastalking.api_key'),
        'username' => env('AT_USERNAME', 'africastalking.username'),
    ]

Nexmo 驱动程序

此驱动程序通过 Nexmo 消息服务发送消息。它非常可靠,能够向全球的手机发送消息。

    return [
        'driver' => 'nexmo',
        'from' => 'Company Name',
        'nexmo' => [
            'api_key'       => 'Your Nexmo API Key',
            'api_secret'    => 'Your Nexmo API Secret',
            'encoding'      => 'unicode', // Can be `unicode` or `gsm`
        ]
    ]; 

Twilio 驱动程序

此驱动程序通过 Twilio 消息服务发送消息。它非常可靠,能够向全球的手机发送消息。

    return [
        'driver' => 'twilio',
        'from' => '+15555555555', //Your Twilio Number in E.164 Format.
        'twilio' => [
            'account_sid' => 'Your SID',
            'auth_token' => 'Your Token',
            'verify' => true,  //Used to check if messages are really coming from Twilio.
        ]
    ];

发送 SMS

在一切设置正确的情况下,发送 SMS 通知将非常简单

use Elimuswift\SMS\Facades\SMS;

SMS::send('My first SMS message', [], function ($sms) {
	$sms->to('07xxxxxxxx');
}); 

多个接收者

向多个联系人发送

use Elimuswift\SMS\Facades\SMS;
$contacts = ['0711xxxxxx', '0722xxxxxx', '0701xxxxxx'];

SMS::send('My bulk SMS notification', [], function ($sms) use($contacts) {
	return array_map(function ($to) use($sms) {
		$sms->to($to);
	}, $contacts);	
}); 

发送 Blade 视图

您还可以使用视图来发送 SMS 通知,第一个参数是您希望使用的视图文件。第二个是您希望传递给视图的数据。最后一个参数是设置消息闭包上所有选项的回调。

use App\Order;

$order = Order::with('user')->first();

SMS::send('sms.order-shiped', compact('order'), function($sms) use($order) {
    $sms->to($order->user->phone_number);
});

驱动程序

驱动方法将在运行时切换提供程序。

//Will send through default provider set in the config file.
SMS::send('Your SMS Message', [], function($sms) {
    $sms->to('+15555555555');
});

SMS::driver('nexmo');

//Will send through Nexmo
SMS::send('Your SMS Message', [], function($sms) {
    $sms->to('+15555555555');
});

使用 Laravel 通知

该包附带了一个用于使用 Laravel 通知系统发送 SMS 消息的通知频道。要开始,请将 routeNotificationForSMS() 方法添加到您的可通知对象中。此方法应返回可通知对象的电话号码。

    /**
     * Get the notification identifier for SMS.
     *
     * @return string
     */
    public function routeNotificationForSMS()
    {
        return $this->attributes['phone_number'];
    }

发送通知

现在您可以在通知中的 via() 方法内使用此频道

use Elimuswift\SMS\Chennels\SMSChannel;
use Elimuswift\SMS\Notifications\SMSMessage;
use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return [SMSChannel::class];
    }

    public function toSms($notifiable)
    {
        return (new SMSMessage())
            ->content("Your {$notifiable->service} account was approved!");
    }
}

您还可以将通知作为 blade 视图发送。

public function toSms($notifiable)
{
    return (new SMSMessage('path.to.view'))
         ->viewData(['foo' => 'Bar', 'baaz' => 'blah']);
}